如何使用jquery禁用提交按钮

时间:2021-12-10 20:33:31

I have a simple form to check the value of post code entered by the user against a variable,

我有一个简单的表格来检查用户针对变量输入的邮政编码的值,

I'm trying to disable the submit button and do the check and give alert message, I can't figure out what I'm doing wrong, any help would be appreciated.

我正在尝试禁用提交按钮并进行检查并发出警告消息,我无法弄清楚我做错了什么,任何帮助都将不胜感激。

  <form id="post_code">
        <input name="textfield" type="text" id="postcode" size="14" maxlength="8" />
        <input type="image" id="submit_postcode" src="images/B_go.png" alt="Submit Postcode" width="59" height="24" border="0" />
  </form>


$(function() {

   $('form#post_code').submit(function() {

    var entered_post_code = $('form#post_code input#postcode').val();
    var post_code = "CV";
    if (entered_post_code == post_code) {
        alert("post code is ok");
        return true;
    }else {
        alert("post code is not ok");
        return true;
    }
    return false;   
});

});

});

6 个解决方案

#1


73  

The W3C recommends to set that attribute to disabled="disabled", so:

W3C建议将该属性设置为disabled =“disabled”,因此:

$('#submit_postcode').attr('disabled', 'disabled');

Re-enabling can be done by removing the attribute:

可以通过删除属性来重新启用:

$('#submit_postcode').removeAttr('disabled');

Setting the attribute to any value causes it to be disabled, even .attr('disabled', 'false'), so it has to be removed.

将属性设置为任何值会导致它被禁用,甚至.attr('disabled','false'),因此必须将其删除。

#2


4  

$('#submit_postcode').attr('disabled', 'disabled');

#3


1  

If the check matches you return true, and if it doesn't match … you also return true. The return false line is never reached.

如果匹配,则返回true,如果匹配,则返回true。永远不会达到返回假行。

You probably want to change one of your trues to false, and eliminate the line outside the if/else block.

您可能希望将其中一个trues更改为false,并消除if / else块之外的行。

#4


1  

This is the code I use to disable or re-enable a control:

这是我用来禁用或重新启用控件的代码:

function handleControlDisplay(className, foundCount, checked) {



    var button = jQuery(className);



    if (foundCount > 0 && foundCount == checked) {

        // enable

        button.removeAttr("disabled");

    }

    else {

        // set the disabled attribute

        button.attr("disabled", "true");

    };

}

#5


0  

var entered_post_code = $('form#post_code input#postcode').val();
// rewrite as
var entered_post_code = $("#post_code input[id=postcode]").val();

You should return false if post code is not ok. no?

如果邮政编码不正确,您应该返回false。没有?

probably your if it never solves as true, and it returns as false always. The usage of submit() function is correct.

可能是你的,如果它从来没有解决为真,它总是返回为假。 submit()函数的用法是正确的。

#6


-3  

//disable
$('#submit_postcode').attr('disabled', true);

//re-enable
$('#submit_postcode').removeAttr('disabled');

#1


73  

The W3C recommends to set that attribute to disabled="disabled", so:

W3C建议将该属性设置为disabled =“disabled”,因此:

$('#submit_postcode').attr('disabled', 'disabled');

Re-enabling can be done by removing the attribute:

可以通过删除属性来重新启用:

$('#submit_postcode').removeAttr('disabled');

Setting the attribute to any value causes it to be disabled, even .attr('disabled', 'false'), so it has to be removed.

将属性设置为任何值会导致它被禁用,甚至.attr('disabled','false'),因此必须将其删除。

#2


4  

$('#submit_postcode').attr('disabled', 'disabled');

#3


1  

If the check matches you return true, and if it doesn't match … you also return true. The return false line is never reached.

如果匹配,则返回true,如果匹配,则返回true。永远不会达到返回假行。

You probably want to change one of your trues to false, and eliminate the line outside the if/else block.

您可能希望将其中一个trues更改为false,并消除if / else块之外的行。

#4


1  

This is the code I use to disable or re-enable a control:

这是我用来禁用或重新启用控件的代码:

function handleControlDisplay(className, foundCount, checked) {



    var button = jQuery(className);



    if (foundCount > 0 && foundCount == checked) {

        // enable

        button.removeAttr("disabled");

    }

    else {

        // set the disabled attribute

        button.attr("disabled", "true");

    };

}

#5


0  

var entered_post_code = $('form#post_code input#postcode').val();
// rewrite as
var entered_post_code = $("#post_code input[id=postcode]").val();

You should return false if post code is not ok. no?

如果邮政编码不正确,您应该返回false。没有?

probably your if it never solves as true, and it returns as false always. The usage of submit() function is correct.

可能是你的,如果它从来没有解决为真,它总是返回为假。 submit()函数的用法是正确的。

#6


-3  

//disable
$('#submit_postcode').attr('disabled', true);

//re-enable
$('#submit_postcode').removeAttr('disabled');