Migrating jQuery 1.8.3 to 1.9.0 - Replacing deprecated .attr()

久未见 提交于 2019-12-12 03:32:45

问题


A site I'm working on was built using jQuery v1.8.3. I'm attempting to upgrade to v1.9.0. Using the jQuery Migration plug-in (v.1.0.0), I'm getting messages in the console saying .attr() is deprecated.

In the .js file these two lines are being used:

var branchID = $('select#ddlBranches').attr('value');
$('select#ddlBranches').attr('value', branchID).change();

They're intended to get the value (which is a record ID) of the current selection in the dropdown, and fire the change event on the dropdown, setting it to the indicated value.

The first line results in this message in the console: "property-based jQuery.fn.attr('value') is deprecated"

The second line causes this to show in the console: "property-based jQuery.fn.attr('value', val) is deprecated"

The Upgrade guide discusses .attr(), but I don't see anywhere that mentions what to replace it with.

What am I supposed to replace .attr() with to accomplish the same functionality?

Thanks for anyone's help.


回答1:


If you want to get/set the value, use the .val() function:

var branchID = $('select#ddlBranches').val();
$('select#ddlBranches').val(branchID).change();


来源:https://stackoverflow.com/questions/14602773/migrating-jquery-1-8-3-to-1-9-0-replacing-deprecated-attr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!