【问题标题】:Javascript name array input selectors (name[sometext])Javascript 名称数组输入选择器 (name[sometext])
【发布时间】:2012-03-04 08:45:15
【问题描述】:

如何在不分配 ID 的情况下在 jQuery 中正确选择这样的命名输入?

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

【问题讨论】:

  • 您能否举例说明当您有多个包含 HTML 的隐藏输入元素时什么不起作用?从您的示例中不清楚。

标签: javascript jquery css-selectors


【解决方案1】:

我很确定您应该将任何属性选择器的值放在引号中,其中包括在 CSS 中具有含义的符号。像这样:

input[type=hidden][name='person[first]']

【讨论】:

    【解决方案2】:

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

    $("input[type=hidden][name='person[first]']").val();
       // ---------------------^-------------^
    

    来自the docs for attribute-equals-selector...

    属性属性名称。

    一个属性值。可以是不带引号的单个单词或带引号的字符串。

    因为你不止一个词,所以应该引用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2013-02-05
      • 1970-01-01
      • 2011-01-05
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多