單選框&復選框方法

1.單選框
html代碼

<div class="radio-inline">
  <input type="radio"  name="radio2" value="1"/>
  <label for="order1">是</label>
</div>
<div class="radio-inline">
  <input type="radio"  name="radio2" value="0" checked/>
  <label for="order2">否</label>
</div>
<!--這是禁選狀態(tài)的單選框-->
<div class="radio-inline">
  <input type="radio"  name="radio2" value="2" disabled/>
  <label for="order3">否</label>
</div>

js代碼

  //獲取單選框選中的值
  $("input[name='radio2']:checked").val();
  //設置其中一個為選中狀態(tài)
  $("input:radio[value='0']").attr('checked', 'checked');
  $("input:radio[value='0']").attr('checked', true);
  //設置單選框為禁選
  $("input:radio[value='0']").prop("disabled", true);
    //取消單選框選中狀態(tài)
    $("input:radio").removeAttr("checked");
    $("input:radio").attr('checked', false);
    //遍歷radio
    $('input:radio').each(function(index, domEle) {
        console.log($(domEle).val())
    });
    //單選框點擊事件
    $('input[name="radio2"]').change(function() {
        console.log($(this).val())
    })

2.復選框
html代碼

<label>input復選1組:</label>
<input type="checkbox" name="checkbox1" value="checkbox復選1" />checkbox復選1
<input type="checkbox" name="checkbox1" value="checkbox復選2" disabled/>checkbox復選2
<input type="checkbox" name="checkbox1" value="checkbox復選3" checked />checkbox復選3

js代碼

//選中某一項
$("input[name='checkbox1'][value='checkbox復選3']").prop("checked", true); 
 //取消選中某一項
$("input[name='checkbox1'][value='checkbox復選3']").removeAttr("checked");
 //設置某一項為禁選
 $("input[name='checkbox1'][value='checkbox復選3']").prop("disabled", true);
 //獲取選中的值
var checkArr = [];
$('input[name="checkbox1"]:checked').each(function() {
    checkArr.push($(this).val());
 });
 console.log(checkArr)

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容