2016-06-10 150 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();