jquery增加和移除屬性
阿新 • • 發佈:2018-11-14
1.HTML結構
<div class="input-box"> <div> <input type="radio" id="desc1" name="descID" value="1" checked> <label class="" for="desc1">使用文章連結</label> </div> <input id="artLink" type="text" name="art_link" placeholder="地址"> </div> <div class="input-box"> <div> <input type="radio" id="desc2" name="descID" value="2"> <label class="" for="desc2">內容</label> </div> <div> <textarea name="" id="txt" cols="30" rows="10" disabled></textarea> </div> </div>
2.js
$("input:radio").click(function(){ let textareaDom = $("#txt"); let artLinkDom = $("#artLink"); let radioVal = $(this).val(); switch(radioVal){ case '1': textareaDom.attr("disabled",'disabled'); //增加屬性 break; case '2': $('#txt').removeAttr("disabled"); //移除屬性 break; default : console.log('default'); } });