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

2018-07-23  本文已影响0人  寂静台风

原因是:需要用prop代替attr
那么,什么时候使用attr(),什么时候使用prop()?
http://wenzhixin.net.cn/2013/05/24/jquery_attr_prop 有介绍
根据官方的建议:具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()

image.png

错误的代码:
function selectedPostion(target, index) {
target.find('option').attr("selected", false);
target.find([value=${index}]).attr('selected', true);
}

改正后:
function selectedPostion(target, index) {
target.find('option').prop("selected", false);
target.find([value=${index}]).prop('selected', true);
}

上一篇 下一篇

猜你喜欢

热点阅读