数组子字段的jQuery选择器?

时间:2012-04-15 04:55:04

标签: jquery arrays selector field

当名称在数组中声明一个子字段时,jQuery选择对象的正确方法是什么?

我大力尝试:

$('select[name=field[subfield]]').change(function(){
  alert('houston we have contact');
});

DOM对象是:

<select name="field[subfield]">
  <option>..</option>
  <option>..</option>
  <option>..</option>
</select>

2 个答案:

答案 0 :(得分:3)

尝试添加引号:

$('select[name="field[subfield]"]').change(function(){
  alert('houston we have contact');
});

工作演示:http://jsfiddle.net/hHHMS/

答案 1 :(得分:0)

按照eZaktos的回答。如果使用相同的引号,则反斜杠。

Single qoutes:

  $('select[name=\'field[subfield]\']').change(function(){
    alert('houston we have contact');
  });

双引号:

  $("select[name=\"field[subfield]\"]").change(function(){
    alert('houston we have contact');
  });

双引号和单引号:

  $("select[name='field[subfield]']").change(function(){
    alert('houston we have contact');
  });

单引号和双引号:

$('select[name="field[subfield]"]').change(function(){
  alert('houston we have contact');
});