jquery - <option> 上的 val() 返回文本而不是 ie7 中的值

标签 jquery

$(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/

相关文章:

javascript - 无法让 vimeo 视频与 fancybox 一起使用 - 我觉得我已经尝试了一切

javascript - 使用 jQuery Mobile 时无法以编程方式选中复选框

jquery - 如何让 CSS 动画在 JQuery 或页面加载后等待加载

javascript - 如何将对象添加到 jquery 结果对象?

JQuery - 添加超过 1 个附件

php - 选择菜单不显示正确的选项

jquery - 将 '&' 作为查询字符串中参数值的一部分传递的标准方法

JavaScript 在 DOM 中移动元素

javascript - 使用鼠标单击获取 X/Y 坐标

javascript - Flexslider(jQuery): Pausing automatic animation