$(this).find('select option:selected').each(function (i, val) {
var selectedValue = $(val).val(); //this returns the text from the option instead of the value
});
这是 ie7 中的已知问题吗?我没找到相关信息?可能的解决方法?谢谢
最佳答案
根据 http://api.jquery.com/val/ 的文档
.val()
仅适用于
The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of
<select multiple="multiple">
elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.
为了实现你想要的,你可以迭代 select
并调用.val()
在上面,您的原始代码实际上正在调用 val()
上option
,不是实际的select
元素,这就是它不起作用的原因。
$(this).find('select').each(function (i, val) {
var selectedValue = $(this).val();
});
val()
还有一个额外的好处是能够在 multiple
的情况下返回值数组选择:(强调我的)
The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of
<select multiple="multiple">
elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.
关于jquery - <option> 上的 val() 返回文本而不是 ie7 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10296213/