2017-10-23 09:40
浏览 549


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;

      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');

图片转代码服务由CSDN问答提供 功能建议

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

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

但问题是,对于该文本框的每个键,提交按钮首先启用,然后变为禁用。 最后还可以。 但是,如果HTML变量是 null ,我试图摆脱为每个键启用提交按钮。 其余代码没问题。 register-submit2 是提交按钮的ID。 任何人都可以帮助我吗?

 $(“#captcha”)  .keyup(function(e){
 var captcha = $(“#captcha”)。val(); 
 var datastring ='captcha ='+ captcha; 
 $ .ajax({
 type:  “POST”,
 url:“my_url / ErrorProcessing.php”,
 if(html ==“输入错误的文字”){
 $('  #register-submit2')。prop('disabled','true'); 
} else {
} \  n} 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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");
    解决 无用
    打赏 举报

相关推荐 更多相似问题