dougu3591 2017-10-23 01: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 01: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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部