简体   繁体   English

为什么启用和禁用按钮不起作用?

[英]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. captcha是文本框的id。

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 . 对于此文本框中的每个键,将有一个AJAX请求被发送到名为ErrorProcessing.php的文件。然后它将提供一个HTML变量,该变量是"wrong text entered"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 . 但是,如果HTML变量为null我试图摆脱为每个键启用提交按钮。 The rest of the code is okay. 其余代码没问题。 register-submit2 is the id of the submit button. register-submit2是提交按钮的id。 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. 使用带引号的true/false String form will be considered as true. 字符串形式将被视为真实。

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

Or you can skip the if using 或者你可以跳过if使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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