简体   繁体   中英

Why is enabling and disabling a button not working?

I am trying to enable a submit button for a form only if the user has input the correct captcha (which is displayed as a image) value inside a textbox. captcha is the id of the textbox.

For each key up on this textbox there will be an AJAX request which is sent to a file called a ErrorProcessing.php.Then it will provide a HTML variable which is either "wrong text entered" or null . The submit button then gets enabled only based on that value. This works.

However the problem is that for every key up on that textbox submit button first becomes enabled and then becomes disabled. In the end it is okay. But I am trying to get rid of the enabling the submit button for every key up if the HTML variable is null . The rest of the code is okay. register-submit2 is the id of the submit button. Can any one help me?

$(document).ready(function() {
  $("#captcha").keyup(function(e) {
    var captcha = $("#captcha").val();
    var datastring = 'captcha=' + captcha;

    $.ajax({
      type: "POST",
      url: "my_url/ErrorProcessing.php",
      data: datastring,
      success: function(html) {
        if (html == "wrong text entered") {
          $('#register-submit2').prop('disabled', 'true');
        } else {
          $('#register-submit2').prop('disabled', 'false');
        }
      }
    });
  });
});

Use true/false without quotes. String form will be considered as true.

$('#register-submit2').prop('disabled', false);

Or you can skip the if using

$('#register-submit2').prop('disabled', html == "wrong text entered");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM