jquery中怎么設置一個radio為選中狀態
JS怎么判斷 radio是否被選中
需要準備的材料分別有:電腦、html編輯器、瀏覽器。
1、首先,打開html編輯器,新建html文件,例如:index.html,輸入問題基礎代碼。
2、在index.html中的<script>標簽,輸入js代碼:
if ($("input[type='radio']:checked").val()) {
$('body').append('被選中');
} el {
$('body').append('未被選中');
}
3、瀏覽器運行index.html頁面,此時js打印了radio是否被選中的判斷結果。
js判斷radio是否被選中
思路:遍歷radio對象,通過checked進行判斷radio是否選中,選中的項會有checked屬性,判斷選中后,可進行后續操作。
示例代碼如下。
<!--html部分-->
<p>
<labelfor="gender">性別:
<inputtype="radio"name="gender"value="male">男
<inputtype="radio"name="gender"value="female">女
</label>
<buttonid="sub"type="button">提交</button>
</p>
<!--script部分-->
<script>
varoRadio=document.getElementsByName("gender"),//獲取radio,是個數組
oButton=document.getElementById("sub"),//獲取按鈕
hasValue=fal,//定義變量
checkedVal="";
functiongetVal(){//獲取radio選中值函數
for(i=0;i<oRadio.length;i++){//循環數組
if(oRadio[i].checked){//判斷當前項是否被選中
//已選中的操作,獲取選中的值
hasValue=true;
checkedVal=oRadio[i].value;
//returncheckedVal;
}el{
hasValue=fal;
}
}
}
oButton.onclick=function(){
getVal();
console.log(checkedVal);//checkedVal即是radio的選中值
console.log(hasValue);//hasValue如果是true,則radio有選中值,否則沒有值
};
</script>
javascript怎么選中radio
怎么讓bootstrap的radio選中
jquery中怎么設置一個radio為選中狀態?
設置選中方法,代碼如下:
$("input[name='名字']").get(0).checked=true;
$("input[name='名字']").attr('checked','true');
$("input[name='名字']:eq(0)").attr("checked",'checked');
$("input[name='radio_name'][checked]").val(); //獲取被選中Radio的Value值
本文發布于:2023-02-28 19:10:00,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/167760165553536.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:radio選中.doc
本文 PDF 下載地址:radio選中.pdf
| 留言與評論(共有 0 條評論) |