Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

how to disable submit button with jquery

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;   
});

});

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

$('#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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...