Ben*_*nee 21 javascript jquery css-selectors
如何在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');
Run Code Online (Sandbox Code Playgroud)
小智 36
将属性值放在引号中...
$("input[type=hidden][name='person[first]']").val();
// ---------------------^-------------^
Run Code Online (Sandbox Code Playgroud)
来自属性等于选择器的文档 ......
属性属性名称.
value属性值.可以是不带引号的单个单词或带引号的字符串.
由于您不仅仅有一个单词,因此应该引用它.