我想编写一个程序来通过删除属性jQuery 代码启用和禁用复选框上的按钮。
我试过了,但没有用。
$(document).ready(function() {
$('#myCheckBox').on('change', function() {
var x = $(this).attr('value');
if (x == 'on') {
$('#myButton').removeAttr('disabled');
} else if (x == undefined) {
$('#myButton').attr('disabled');
}
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<label for="myCheckBox">
I agree with terms and conditions
<input type="checkbox" id="myCheckBox" />
</label>
<br />
<br />
<input type="button" id="myButton" value="Submit" disabled />Run Code Online (Sandbox Code Playgroud)
我是 jQuery 的初学者,如有任何疑问,请在下面发表评论。
使用.is(':checked')而不是检查值。还替换:
$('#myButton').attr('disabled'); //Get the value
Run Code Online (Sandbox Code Playgroud)
经过 :
$('#myButton').attr('disabled','disabled'); //Set the value
Run Code Online (Sandbox Code Playgroud)
来设置值。
注意:您可以使用以下命令prop()来执行此操作:
$("#myButton").prop('disabled', !$(this).is(':checked'));
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
attr()/removeAttr()片段:
$('#myButton').attr('disabled'); //Get the value
Run Code Online (Sandbox Code Playgroud)
$('#myButton').attr('disabled','disabled'); //Set the value
Run Code Online (Sandbox Code Playgroud)
prop()片段:
$("#myButton").prop('disabled', !$(this).is(':checked'));
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function(){
$('#myCheckBox').on('change',function(){
if( $(this).is(':checked') ){
$('#myButton').removeAttr('disabled');
}else{
$('#myButton').attr('disabled','disabled');
}
});
});Run Code Online (Sandbox Code Playgroud)