tags:

views:

1000

answers:

5

Hi,

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

});

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

//re-enable
$('#submit_postcode').removeAttr('disabled');
redsquare
Hi, is the code above to be replaced with $('form#post_code').submit(function() {}thanks
amir
disabled attribute should be set to 'disabled' not 'true'
David Liddle
David, either work my friend
redsquare
One works because it is right. One works because the browser you are testing in compensates for the error.
David Dorward
hmm. It works x-browser therefore it is not wrong and does not deserve a downvote
redsquare
A: 

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

David Liddle
+2  A: 

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.

Mythica
A: 
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?

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

Elzo Valugi
A: 

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

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

David Dorward