检查后禁用复选框
问题描述:
请首先检查后禁用复选框再次检查时需要帮助。检查后禁用复选框
这是我迄今为止所做的。
<label class="checkbox-inline"><input type="checkbox" id="checked" onclick="check();"></label>
<script type="text/javascript">
function check() {
if($("#checked").is(":checked")){
alert("Thanks for Attending");
$(this).attr('disabled','disabled');
}
}
}
</script>
答
请检查此片段。
function check() {
if($("#checked").is(":checked")){
alert("Thanks for Attending");
//Code to disable checkbox after checked
$('#checked').attr('disabled', true);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkbox-inline"><input type="checkbox" id="checked" onclick="check();"></label>
+0
感谢它的完美。 –
答
变化
$(this).attr('disabled','disabled');
to
$('#checked').attr('disabled', true);
由于$(这)不是指的复选框,因为你是在函数体中。
答
香草JS的选择,因为你正在试图将其与jQuery结合起来,而你不能这样做:
function check() {
var el = document.getElementById("checked");
if (el.checked) {
el.disabled = true;
}
}
如果您仍然需要jQuery的版本棒.prop()
用法:
function check() {
var $el = $("#checked");
if ($el.is(":checked")) {
$el.prop("disabled", true);
}
}
可能的重复吃了[禁用复选框,并取消选中它使用jQuery](http://stackoverflow.com/questions/21108486/disabling-check-box-and-uncheck-it-using-jquery) – Idan