$('select option:selected').text()和Attr.specified已弃用警告 [英] $('select option:selected').text() and Attr.specified is deprecated warning
问题描述
在我的项目中,我需要使用<select>
中的所选值和文本来更新主干模型.
In my project I need to update a backbone model with both the selected value and text from a <select>
.
为此,我打电话给
model.set 'value', $('select').val()
model.set 'value_text', $('select option:selected').text()
(我也在使用咖啡脚本).由于我当前正在使用的jQuery v2.0.3中存在一些问题,我收到此警告:
(I am using coffee script as well). Because of some problem in jQuery v2.0.3 which I am currently using I am receiving this warning:
Attr.specified已弃用.其值始终为真.
Attr.specified is deprecated. Its value is always true.
我知道对此警告有疑问,但我想提出完全不同的问题:
I know there were questions on SO about this warning, but I want to ask something completely different:
由于在接下来的几个月内无法更新到新版本的jQuery(可能会解决问题),我想问一下是否还有其他方法可以接收所选选项的文本,而不是上面使用的文本.如果没有其他人使用jQuery,我不反对纯粹的JS解决方案.
Since updating to a newer version of jQuery (where the problem might be fixed) is not possible in next few months I would like to ask whether there is other way round to receive the selected option's text instead of that used above. I am not against pure JS solution if there is no other using jQuery.
我们非常感谢您的帮助.
Any help is highly appreciated.
编辑:警告是由于询问该选项上是否存在selected
属性引起的.
EDIT for @KevinB: The warning is caused by asking whether there is selected
attribute on that option.
推荐答案
要在不做任何其他更改的情况下修复"它,您只需使用.filter.
To *fix* it without changing anything else, you can just use .filter.
$('select option').filter(function (i) { return this.selected; }).text();
这篇关于$('select option:selected').text()和Attr.specified已弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!