Javascript 名称数组输入选择器 (name[sometext])

标签 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');

最佳答案

将属性值放在引号中...

$("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/

相关文章:

javascript - Knockout 不在 mvc View 中显示数据

javascript - 单词搜索游戏。如何搜索网格并突出显示结果?

javascript - 如何从异步调用返回响应?

javascript - jQuery - 针对 sibling 、 parent 、后代和其他家庭动物

javascript - 使用 Mocha 在 NodeJS 和 MongoDB 中测试用户模型时没有结果

javascript - 在Extjs中提示警告消息框

javascript - 有人能发现这段代码的错误吗?

javascript - 如何禁用 jQuery UI Datepicker 上日期选择器的关闭

asp.net - css 结构伪类

javascript - 使用 ExtJS 获取特定类型的所有元素