select中的option設(shè)置了selected屬性但是沒有選中

原因是:需要用prop代替attr
那么,什么時(shí)候使用attr(),什么時(shí)候使用prop()?
http://wenzhixin.net.cn/2013/05/24/jquery_attr_prop 有介紹
根據(jù)官方的建議:具有 true 和 false 兩個(gè)屬性的屬性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()

image.png

錯(cuò)誤的代碼:
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);
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,134評(píng)論 0 2
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,155評(píng)論 0 3
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,527評(píng)論 19 139
  • 一鳴也不驚人閱讀 348評(píng)論 0 2
  • 太行山方八百許里,跨豫晉兩省,但屬晉。猶天投一大山,任世人劃分,天無心過問。群山或綿延起伏,或遺世獨(dú)立,或犬牙...
    四月的麥田閱讀 527評(píng)論 0 0

友情鏈接更多精彩內(nèi)容