dougu3591 2017-10-23 09:40
浏览 661
已采纳

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

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');
        }
      }
    });
  });
});
  • 写回答

1条回答

  • douchan4674 2017-10-23 09:51
    关注

    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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败