[英]How do i disable a submit button when checkbox is uncheck?
I have something here that cannot seem to help me disable the submit button. 我这里有些东西似乎无法帮助我禁用提交按钮。 any ideas? 有任何想法吗?
<input type="checkbox" checked="checked" id="checky"><a href="#">terms and conditions</a>
<input type="submit" id="postme" value="submit">
$('#checky').click(function(){
if($(this).checked == false){
$('#postme').attr("disabled","disabled");
} else {
$('#postme').removeAttr('disabled');
}
});
You should be checking for the attribute checked
and not the method. 你应该检查属性checked
,而不是方法。
Updated fiddle : http://jsfiddle.net/8YBu5/7/ 更新小提琴: http : //jsfiddle.net/8YBu5/7/
$('#checky').click(function(){
if($(this).attr('checked') == false){
$('#postme').attr("disabled","disabled");
} else {
$('#postme').removeAttr('disabled');
}
});
EDIT 编辑
Remember that this code needs to be wrapped inside $(document).ready()
or put at the bottom of your html code, otherwise your JS will bind itself to DOM elements that have not been loaded yet and will fail. 请记住,这段代码需要包含在$(document).ready()
或者放在html代码的底部,否则你的JS会将自己绑定到尚未加载的DOM元素并且会失败。
Wrapping it in .ready() 将它包装在.ready()中
<script type='text/javascript'>
$(document).ready(function(){
$('#checky').click(function(){
if($(this).attr('checked') == false){
$('#postme').attr("disabled","disabled");
} else {
$('#postme').removeAttr('disabled');
}
});
});
</script>
This code can be put anywhere in the document and will only trigger once the DOM is ready so you don't have to worry about premature triggers. 此代码可以放在文档中的任何位置,只有在DOM准备好后才会触发,因此您不必担心过早的触发器。
Putting it at the bottom of your document 将它放在文档的底部
<!-- some HTML -->
<script type='text/javascript'>
$('#checky').click(function(){
if($(this).attr('checked') == false){
$('#postme').attr("disabled","disabled");
} else {
$('#postme').removeAttr('disabled');
}
});
</script>
</body>
</html>
If you're putting it at the bottom of your document, you don't need to wrap it in .ready()
because the javascript will not be read until everything else has loaded. 如果你把它放在文档的底部,你不需要将它包装在.ready()
因为在加载其他所有内容之前不会读取javascript。 There is a performance boost in this method (if you have a lot of JS) 这种方法有一个性能提升(如果你有很多JS)
You can use either one of these methods to make sure that your JS binds event handling methods to DOM elements only after they have finished loading. 您可以使用这些方法中的任何一种来确保JS仅在完成加载后才将事件处理方法绑定到DOM元素。
Before jQuery 1.6 attr
was OK, after 1.6 you must use prop
. 在jQuery 1.6 attr
之前,在1.6之后你必须使用prop
。
$('#checky').click(function(){
$('#postme').prop('checked', !$(this).checked);
})
Attributes and properties are different things, but unfortunately jQuery took a long time to differentiate between them. 属性和属性是不同的东西,但不幸的是jQuery花了很长时间来区分它们。
See also: .prop() vs .attr() 另请参见: .prop()vs .attr()
if(!$(this).is(':checked') ...
Try this 试试这个
$(document).ready(function(){
$("#postme").attr("disabled","disabled");
$("#checky").click(function(){
if($("#checky").is(":checked")){
$("#postme").removeAttr("disabled");
}
else{
$("#postme").attr("disabled","disabled");
}
});
})
Try this way 试试这种方式
$('#checky').click(function(){
if(this.checked == false){
$('#postme').attr("disabled","disabled");
}
else {
$('#postme').removeAttr('disabled');
}
});
Add to the button #postme
a click event that checks if the check box is checked. 添加按钮#postme
一个单击事件,检查是否选中了复选框。 If it is checked, return false, otherwise return true. 如果选中,则返回false,否则返回true。
$('#postme').click( function () {
if ( !$('#checky').attr('checked') ) {
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.