如果取消选中复选框,我如何禁用提交按钮? [英] How do i disable a submit button when checkbox is uncheck?

查看:127
本文介绍了如果取消选中复选框,我如何禁用提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有某些这里似乎无法帮助我禁用提交按钮。任何想法?

 < input type =checkboxchecked =checkedid =checky>< a href =#>条款< / a> 
< input type =submitid =postmevalue =submit>

$('#checky')。click(function(){
if($(this).checked == false){
$('#postme') .attr(disabled,disabled);
} else {
$('#postme')。removeAttr('disabled');
}
}


解决方案

您应该检查属性选中而不是方法。



更新了小提琴: http://jsfiddle.net/8YBu5/7/

  $('#checky')。单击(function(){
if($(this).attr('checked')== false){
$('#postme')。attr(disabled,disabled) ;
} else {
$('#postme')。removeAttr('disabled');
}
});

EDIT



<请记住,这个代码需要包装在 $(document).ready()或放在你的html代码的底部,否则你的JS会绑定到DOM



$ b

$ b

 < script type ='text / javascript'> 
$(document).ready(function(){
$('#checky')。click(function(){
if($(this).attr('checked' == false){
$('#postme')。attr(disabled,disabled);
} else {
$('#postme')。removeAttr disabled);
}
});
});
< / script>

此代码可以放在文档的任何位置,只有在DOM准备就绪时才会触发不必担心过早触发。



将其放在文档底部

 <! -  some HTML  - > 
< script type ='text / javascript'>
$('#checky')。click(function(){
if($(this).attr('checked')== false){
$(' ).attr(disabled,disabled);
} else {
$('#postme')。removeAttr('disabled');
}
} ;
< / script>
< / body>
< / html>

如果将它放在文档底部,则不需要换行在 .ready(),因为javascript将不会被读取,直到一切装载。这种方法有一个性能提升(如果你有很多JS)



你可以使用这些方法之一来确保你的JS绑定事件处理方法仅在DOM元素之后完成加载。


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.

Updated fiddle : 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.

Wrapping it in .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.

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. There is a performance boost in this method (if you have a lot of 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.

这篇关于如果取消选中复选框,我如何禁用提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆