使用jQuery動(dòng)態(tài)設(shè)置單選框的選中效果
一、需要實(shí)現(xiàn)的效果
這里使用jQuery來(lái)實(shí)現(xiàn)。需要實(shí)現(xiàn)的效果如下:當(dāng)下拉條改變時(shí),單選框選中的值隨之變化。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>動(dòng)態(tài)設(shè)置單選框的選中</title> <!-- 作者:Harrison 時(shí)間:2018-12-05 描述:當(dāng)下拉條改變時(shí),動(dòng)態(tài)的設(shè)置單選框的值 --> </head> <body> <select id="sexSelect" style="width: 10%;"> <option value="1">男</option> <option value="2">女</option> </select> 男:<input type="radio" value="1" name="sex" id="sexRadio1" checked/> 女:<input type="radio" value="2" name="sex" id="sexRadio2"/> </body> <script type="text/javascript" src="js/jquery-1.7.2.min.js" ></script> <script type="text/javascript"> $("#sexSelect").change(function(){ //獲取選中的下拉條 var checkedOfSelect = $("#sexSelect").val(); //動(dòng)態(tài)設(shè)置單選框的選中 if(checkedOfSelect == 1){ $("#sexRadio1").prop("checked",true); $("#sexRadio2").prop("checked",false); } if(checkedOfSelect == 2){ $("#sexRadio1").prop("checked",false); $("#sexRadio2").prop("checked",true); } }); </script> </html>
二、注意
•當(dāng)設(shè)置單選框的checked屬性時(shí),要使用jQuery的prop()方法,不能夠使用jQuery的attr()方法,attr()只適合簡(jiǎn)單html元素設(shè)置。
•jQuery的prop()方法,第二個(gè)參數(shù)為布爾值,不能設(shè)置成string類型:
正確:$("#sexRadio1").prop("checked",true);
錯(cuò)誤:$("#sexRadio1").prop("checked","true");
總結(jié)
以上所述是小編給大家介紹的使用jQuery動(dòng)態(tài)設(shè)置單選框的選中效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
使用CSS樣式position:fixed水平滾動(dòng)的方法
這篇文章主要介紹了使用CSS樣式position:fixed水平滾動(dòng)的方法,需要的朋友可以參考下2014-02-02jquery如何判斷表格同一列不同行input數(shù)據(jù)是否重復(fù)
這篇文章主要介紹了jquery如何判斷表格同一列不同行input數(shù)據(jù)是否重復(fù),需要的朋友可以參考下2014-05-05ASP.NET jQuery 實(shí)例11 通過使用jQuery validation插件簡(jiǎn)單實(shí)現(xiàn)用戶登錄頁(yè)面驗(yàn)證功能
從這節(jié)開始,我們開始學(xué)習(xí)如何在ASP.NET控件中使用jQuery validation 插件,首先要用它,必須先了解它有什么用2012-02-02解決JQuery的ajax函數(shù)執(zhí)行失敗alert函數(shù)彈框一閃而過問題
這篇文章主要介紹了解決JQuery的ajax函數(shù)執(zhí)行失敗alert函數(shù)彈框一閃而過問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04基于jquery實(shí)現(xiàn)圖片上傳本地預(yù)覽功能
這篇文章主要介紹了基于jquery實(shí)現(xiàn)圖片上傳本地預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2016-01-01jquery實(shí)現(xiàn)一個(gè)全局計(jì)時(shí)器(商城可用)
這篇文章主要介紹了jquery實(shí)現(xiàn)一個(gè)全局計(jì)時(shí)器,商城一類都可以使用2017-06-06