小心jquery的attr坑

问题:
select中的option设置了selected属性但是没有选中

更详细点是第一次可以选中,第二次看到已经是selected,结果不显示

那么,什么时候使用attr(),什么时候使用prop()?
根据官方的建议:具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()

图片.png

相关的例子如下:

$("#businessUser").find("option:selected").prop("selected", false);
                $("#businessUser").find("option:contains(" + businessUserId + ")").prop("selected", true);

猜你喜欢

转载自blog.csdn.net/weixin_29003023/article/details/107404807