简体   繁体   English

如果取消选中复选框,如何禁用提交按钮?

[英]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') ...

change $(this).checked to if($(this).attr('checked') == false){ $(this).checkedif($(this).attr('checked') == false){

Here you go. 在这里你去。

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.

相关问题 如何取消选中复选框? - How do I uncheck a checkbox? 单击模式对话框按钮后,如何取消选中复选框? - How do I uncheck the checkbox after modal dialog button is clicked? 如果选中任何复选框并在未选中任何复选框时禁用它,如何启用提交按钮? - How can I enable a submit button if any checkbox is checked and disable it when none checkbox is checked? 如何使用 slider 样式复选框禁用和重新启用提交类型按钮? - How do I disable and re-enable a submit type button with a slider style checkbox? 当我点击“提交”按钮时,如何将复选框/单选按钮发布到文本区域? - How do I have a checkbox/radio button posted to a textarea when I hit submit button? 单击按钮时取消选中复选框 - Uncheck the checkbox when clicked On Button 如何在选中单选按钮时取消选中复选框,以及在使用Javascript选中复选框时如何取消选中单选按钮 - How to uncheck checkbox when radio button is checked and uncheck radio button when checkboxes are checked using Javascript 如何取消选中后退按钮上的复选框? - How to uncheck checkbox on back button? 取消选中复选框时如何禁用PHP禁用和清除文本框。 选中/取消选中复选框,具体取决于数据库 - How to disable anPHP disable and clear textbox when checkbox is unchecked. Check/uncheck checkbox depending on database 如何在提交表单时没有任何错误地禁用提交按钮 - how do I disable the submit button when the form gets submitted without any errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM