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

原因是:需要用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);
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容