jquery 代码无法正常工作

jquery code is not working properly

我有一个订单,在提交订单之前,用户必须通过验证码才能货到付款...

我正在使用 jQuery 检查验证码是否正确。

如果输入的验证码有误,我不会让用户使用 preventDefault 提交表单,并会发出错误警报。但是输入正确的验证码后,仍然不让用户提交订单。

这是我的代码...

//for captcha code
      var captcha = null;
      //check if captcha code is right
      $("#apply_captcha").on("click", function(){
        var captchaUrl = '<?php echo base_url(); ?>index.php/checkout/check_captcha/';
        var captcha = $('#userCaptcha').val(); //user entered value for captcha
          $.ajax({
                url: captchaUrl,
                type: 'POST',
                data: 'captcha='+captcha,
                success: function(result)
                {
                    if(result === "success")
                    {
                        captcha = true;
                        $('#captcha_msg').hide();
                        $('#userCaptcha').css('border', '2px solid green');
                    }
                    else
                    {
                        captcha = false;
                        $('#captcha_msg').text("Sorry, Your captcha is wrong.");
                        $('#captcha_msg').css('color', '#cb2700');
                    }
                }
            });
      });

      $('#orderform').submit(function(e)
      {
          if(!pincode_available)
          {
              alert("Sorry, We are not supplying any product at this area right now.");
              e.preventDefault();
          }
          else if(!captcha)
          {
              alert("Sorry, Your captcha is wrong");
              e.preventDefault();
          }
      });

删除 'click' 函数中 'captcha' 变量的 var 关键字。看起来它在提交函数使用全局变量时创建了一个新的局部变量。

P.S:请查看问题的评论,了解我们是如何得出解决方案的。