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

标签 javascript php jquery html forms

仅当用户在文本框中输入了正确的验证码(显示为图像)值时,我才尝试启用表单的提交按钮。 captcha 是文本框的 id。

对于此文本框中的每个按键,都会有一个 AJAX 请求,该请求会发送到名为 ErrorProcessing.php 的文件。然后它将提供一个 HTML 变量,该变量是“输入了错误的文本”null。然后,仅根据该值启用提交按钮。这有效。

但是问题是,对于该文本框上的每个按键,提交按钮首先会被启用,然后会被禁用。最后还好。但如果 HTML 变量为 null,我会尝试取消为每个按键启用提交按钮的功能。其余的代码没问题。 register-submit2 是提交按钮的 id。谁能帮帮我吗?

$(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');
        }
      }
    });
  });
});

最佳答案

使用不带引号的true/false。字符串形式将被视为 true。

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

或者您可以使用

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

关于javascript - 为什么启用和禁用按钮不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46886207/

相关文章:

javascript - 使用 ORM(如 ActiveRecord)为 Web API 定义模型时, 'Type Tables'(如 BOOK_TYPE 表)的重要性

javascript - 在 React 中将数据传递给父级

javascript - 什么是 prototype.constructor 及其在原型(prototype)链中的作用 - Javascript

php - osticket,通过 REST API 创建票证

javascript - 类型错误 : Cannot read property '0' of undefined for an array already defined in google scripts

php - jQuery 使用 id 选择器返回错误值

php - 找不到 laravel 流明输入类

javascript - 选择除一个元素以外的所有元素,它是 jQuery 中的子元素,在 Safari 中不起作用

javascript - 如何使用 JQuery 创建循环动画

php - jquery最接近特定div内的图像查找