0

我需要在选择框的 jQuery 中获取选定的选项文本。我用过

   $('#selectId :selected').text();

它对我来说工作正常。但是我如何通过以下来做到这一点。在这里我正在迭代所有具有特定类的选择框。我使用了以下代码。但它对我不起作用。

 $('.className').each(function(index,value){
    alert($(this :selected).text());
  });

它给出了错误SyntaxError: missing ) after argument list

请帮助我。在此先感谢...

4

3 回答 3

1

那是不正确的语法。你可以使用:

$('.className').each(function(index,value){
    alert($(this).find('option:selected').text());
});

或作为替代方案:

$('.className').each(function(index,value){
    alert($("option:selected", this).text());
});
于 2013-04-24T07:08:46.507 回答
0

请使用这个

 $('.className').each(function(index,value){
    if($(this).is(':checked'))
      { alert($(this).val());}
  });
于 2013-04-24T07:25:11.997 回答
0

See.className本身就是一个集合,因此您可以在change获取option:selected它的事件然后警告它的文本时以这种方式使用它。

$('.className').on('change', function(){
   alert($("option:selected", this).text());
});
于 2013-04-24T07:16:13.807 回答