将 jQuery 1.8.3 迁移到 1.9.0 - 替换已弃用的 .attr() [英] Migrating jQuery 1.8.3 to 1.9.0 - Replacing deprecated .attr()

查看:22
本文介绍了将 jQuery 1.8.3 迁移到 1.9.0 - 替换已弃用的 .attr()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的网站是使用 jQuery v1.8.3 构建的.我正在尝试升级到 v1.9.0.使用 jQuery Migration 插件 (v.1.0.0),我在控制台中收到消息说 .attr() 已被弃用.

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.

在 .js 文件中使用了这两行:

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

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

它们旨在获取下拉列表中当前选择的值(即记录 ID),并在下拉列表上触发更改事件,将其设置为指定值.

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.

第一行在控制台中显示此消息:不推荐使用基于属性的 jQuery.fn.attr('value')"

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

第二行导致在控制台中显示:不推荐使用基于属性的 jQuery.fn.attr('value', val)"

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

升级指南 讨论了 .attr(),但我没有看到任何地方提到用什么来替换它.

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

我应该用什么替换 .attr() 来完成相同的功能?

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

感谢任何人的帮助.

推荐答案

如果要获取/设置 value,请使用 .val() 函数:

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

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

这篇关于将 jQuery 1.8.3 迁移到 1.9.0 - 替换已弃用的 .attr()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆