jquery實現(xiàn)單選按鈕radio選中和取消 使用prop()代替attr()
網(wǎng)絡(luò) 2019/12/20 20:36:22 字體:
大 中 小 瀏覽 5842
<!DOCTYPE html>
<html lang="en">
<head><title>jquery實現(xiàn)單選按鈕radio選中和取消 使用prop()代替attr()</title></head>
<body>
JQuery獲取選中的radio<br>
$('input:radio[name="sex"]:checked')<br><br>
Jquery選中或取消radio<br>
$("#qwbmdanxuan1").attr("checked","checked");<br>
$("#qwbmdanxuan2").attr("checked",false);<br><br>
<input type="radio" name="aaa" value="第 1 個">
第 1 個<br>
<input type="radio" name="aaa" value="第 2 個">第 2 個<br>
<input type="radio" name="aaa" value="第 3 個">第 3 個<br>
<input type="radio" name="aaa" value="第 4 個">第 4 個<br>
<input type="radio" name="aaa" value="第 5 個">第 5 個<br><br>
<button class="btn1">點我除去選中項</button>
<button class="btn2">點我選中第二個</button>
<button class="btn3">點我選中第三個</button>
<button class="btn4">點我選中第四個</button>
<button class="btn5">點我選中第五個</button>
<script language="javascript" type="text/javascript" src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script>
/*//解決選中的單選框無法取消問題
$(":radio").click(
function(){
var nm=$(this).attr("name");
$(":radio[name="+nm+"]:not(:checked)").attr("tag",0);
if($(this).attr("tag")==1){
$(this).attr("checked",false);
$(this).attr("tag",0);
} else{
$(this).attr("tag",1);
}
}
);*/
function de() {
//錯誤示范,均會出現(xiàn)什么各種問題
/*$.each($('input:radio'),function(i,v){
$(v).attr('checked', false);
$(v).removeAttr('checked');
//v.checked = false;
//v.removeAttribute("checked");
})*/
//$("input[name=aaa]").prop("checked",false);
$('input:checked').prop('checked', false);
//$("input[name=aaa]").removeAttr("checked")
}
$('.btn1').click(function () {
de()
})
$('.btn2').click(function () {
//de();
$('input:radio').eq(1).prop('checked', true);
})
$('.btn3').click(function () {
//de();
$('input:radio').eq(2).prop('checked', true);
})
$('.btn4').click(function () {
//de();
$('input:radio').eq(3).prop('checked', true);
})
$('.btn5').click(function () {
//de();
$('input:radio').eq(4).prop('checked', true);
})
</script>
</body>
</html>
- 相關(guān)閱讀
- XHTML+CSS兼容性解決方案小集
- 新鄉(xiāng)市神州國際旅行社
- 計算ASP頁面執(zhí)行時間
- 按鈕復(fù)制文章URL和復(fù)制保留文章的版權(quán)
- javascri獲取頁面實際高度
- 生成隨機字符串的函數(shù),可以做為驗證碼一類的使用
- 根據(jù)頁面模板動態(tài)生成html頁面
- 深山留言板過年回家效果
- 共有0條關(guān)于《jquery實現(xiàn)單選按鈕radio選中和取消 使用prop()代替attr()》的評論
- 發(fā)表評論