2013-04-24 29 views
0

使用这种选择的文本值我需要在选择box.I的jQuery的选择的选项中使用如何让jQuery的

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

它工作正常进行me.But我可怎么办这与以下。这里我遍历所有 选择框有一个特定的类。我使用下面的代码。但它不是为我工作 。

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

这是给错误SyntaxError: missing) after argument list

请帮我.Thanks提前...

回答

1

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

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

或作为一种替代方案:

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

这是为我工作的感谢ü – PSR 2013-04-24 07:13:53

+1

你的第一个答案也为我工作 – PSR 2013-04-24 07:14:53

0

.className是一个集合本身,所以你可以用这种方式对change事件得到它的option:selected然后警报文本的。

$('.className').on('change', function(){ 
    alert($("option:selected", this).text()); 
}); 
0

请使用

$('.className').each(function(index,value){ 
    if($(this).is(':checked')) 
     { alert($(this).val());} 
    }); 
+1

,但我使用select box.How我可以用这 – PSR 2013-04-24 09:12:21