Migrating jQuery 1.8.3 to 1.9.0 - Replacing deprec

2019-09-08 05:08发布

问题:

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();