0

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

我自发尝试:

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

DOM 对象是:

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

2 回答 2

3

尝试添加引号:

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

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

于 2012-04-15T04:59:07.497 回答
0

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

单引号:

  $('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');
});
于 2012-04-15T05:05:38.667 回答