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)