如何在 jQuery 中正确选择这样的命名输入而不分配 ID?
<input name="person[first]" type="hidden">
<input name="person[last]" type="hidden">
// Seems to be acting on multiple hidden elements instead of this named one
// This sets the value of both of those hidden elements in some cases
$("input[type=hidden][name=person[first]]").val('Test');
// So I've changed it to this. Is there a better way?
$("input[name=person[first]]").val('Test');
最佳答案
将属性值放在引号中...
$("input[type=hidden][name='person[first]']").val();
// ---------------------^-------------^
<小时/>
来自 the docs for attribute-equals-selector ...
attribute An attribute name.
value An attribute value. Can be either an unquoted single word or a quoted string.
由于您有多个单词,因此应该将其引用。
关于Javascript 名称数组输入选择器 (name[sometext]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252217/