2016-06-10 148 views
0

直升机,我不能选择元素,你能帮助我吗?在jquery中按名称选择元素

var string = $('input[value="' + min + '"]').prop('name');

我有string = ObjectSize[2];

我需要这个$('input[name=' + string + ']').remove();,但它不工作。

Uncaught Error: Syntax error, unrecognized expression: input:text[name=ObjectSize[2]] 

请帮助我。谢谢

+0

添加的console.log(字符串)到您的代码粘贴输出的地方。需要看看它里面有什么。 – RaV

回答

0

尝试在名称中添加双引号:

$('input[name="' + string + '"]').remove(); 
0

其实你得到超过1元的对象。 试试这个代码:

$('input[type="text"][name="' + string[0] + '"]').remove(); 
0

如果string变量是一个对象(你发现了2种元素的选择)。您不能使用对象$('input[name=' + string + ']').remove();

你需要先检查其对象:

if(typeof string === 'object') { 
    $('input[name="' + string[Object.keys(string)[0]] + '"]').remove(); 
} 
else 
    $('input[name="' + string + '"]').remove();