jquery實(shí)現(xiàn)單選按鈕radio選中和取消 使用prop()代替attr()
網(wǎng)絡(luò) 2019/12/20 20:36:22 字體:
大 中 小 瀏覽 6149
<!DOCTYPE html>
<html lang="en">
<head><title>jquery實(shí)現(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 個(gè)">
第 1 個(gè)<br>
<input type="radio" name="aaa" value="第 2 個(gè)">第 2 個(gè)<br>
<input type="radio" name="aaa" value="第 3 個(gè)">第 3 個(gè)<br>
<input type="radio" name="aaa" value="第 4 個(gè)">第 4 個(gè)<br>
<input type="radio" name="aaa" value="第 5 個(gè)">第 5 個(gè)<br><br>
<button class="btn1">點(diǎn)我除去選中項(xiàng)</button>
<button class="btn2">點(diǎn)我選中第二個(gè)</button>
<button class="btn3">點(diǎn)我選中第三個(gè)</button>
<button class="btn4">點(diǎn)我選中第四個(gè)</button>
<button class="btn5">點(diǎn)我選中第五個(gè)</button>
<script language="javascript" type="text/javascript" src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script>
/*//解決選中的單選框無(wú)法取消問(wèn)題
$(":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() {
//錯(cuò)誤示范,均會(huì)出現(xiàn)什么各種問(wè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)閱讀
- 在圖片未知尺寸情況下,利用CSS把圖片垂直居中于div中心
- 百度小程序開(kāi)發(fā)第一坑tcomponent自定義組件命名坑Compile san component failed. . [SAN ERROR] ROOT>swan-custom-component>view>千萬(wàn)不要用下劃線作為自定義組件名
- 青海眾信國(guó)際旅行社有限公司
- javascript正則的用法與示例
- ASP操作access或sqlserver數(shù)據(jù)庫(kù)的函數(shù)庫(kù)
- 深山行者V1.2旅行社網(wǎng)站管理系統(tǒng)
- uni.openLocation打開(kāi)地圖導(dǎo)航?jīng)]反應(yīng)解決方式(參數(shù)都設(shè)置對(duì)就是打不開(kāi))
- 對(duì)access數(shù)據(jù)表進(jìn)行操作
- 共有0條關(guān)于《jquery實(shí)現(xiàn)單選按鈕radio選中和取消 使用prop()代替attr()》的評(píng)論
- 發(fā)表評(píng)論