$(’select option:selected’)。text()和Attr.specified是不推荐使用的警告

在我的项目中,我需要使用选择的值和文本更新骨干模型。

为此我打电话

 model.set 'value', $('select').val() model.set 'value_text', $('select option:selected').text() 

(我也在使用咖啡脚本)。 由于我目前正在使用的jQuery v2.0.3中的一些问题,我收到此警告:

Attr.specified已弃用。 它的价值永远是真实的。

我知道有关此警告的问题,但我想问一些完全不同的东西:

由于在接下来的几个月内无法更新到更新版本的jQuery(问题可能会得到修复),我想问一下是否有其他方法来接收所选选项的文本而不是上面使用的文本。 如果没有其他人使用jQuery,我不反对纯JS解决方案。

任何帮助都非常感谢。

编辑 @KevinB:警告是由询问该选项是否有selected属性引起的。

要修复它而不改变任何其他东西,你可以使用.filter。

 $('select option').filter(function (i) { return this.selected; }).text(); 

我更喜欢用不同的方法做到这一点。 看代码。

内部视图。

 events: "change input":"updateModel" "change select" : "SelectedItem" //Just for example. selectedItem:(element)-> selectedOption = element.target.selectedOptions alert **selectedOption.item().value** // the value of the selected item. now you can set it on model. updateModel:(element)-> @model.setNew(element.target.name, element.target.value) 

内部模型。

 setNew:(newName, newValue)-> @set newName, newValue 

Html文件。

   

希望能帮助到你。