0

我正在编写一个小脚本来执行正则表达式验证。如果正则表达式不匹配,我禁用提交/“下一步”按钮。它工作正常,但如果在给定的文本字段中没有输入任何内容,我想启用提交/“下一步”按钮。我有一个看起来像这样的函数:

function functionname( code, tograb ){
    $.ajax({
            url : "{% url survey.views.ajax %}",
            data: {
            'r_code': code,
            'qid': '{{ question.id }}',
            'other_text': tograb.value,
            'csrfmiddlewaretoken': '{{ csrf_token }}'
      }, success: function(html){          
        if (html != "OK") {
          $('#Next').attr('disabled', true);
          $(#'span' + String(code)).html(html);
        } else {
          $('#Next').removeAttr('disabled');
           $(#'span' + String(code)).html("");
        }
      }
     });

    if (tograb.value.length == 0) {
          console.log("removing attribute");
          console.log(tograb.value.length);
          $('#Next').removeAttr('disabled');
    }
};

对应的 HTML 如下所示:

  <input type="text" name="1" value = ""
  id = "other_text1"
  onkeyup="functionname('1', this)"
  /> 

这似乎适用于字符串长度超过 1 的所有内容,但如果我删除文本字段中的所有内容,它就不起作用。实际上,它似乎工作了半秒钟,因为它重新启用了提交按钮,但随后又禁用了该按钮。

查看 Firefox 的调试控制台,我看到以下内容:

[14:02:26.757] GET http://myurl/survey/ajax?r_code=1&qid=9&other_text=d&   
csrfmiddlewaretoken=m9s5BxbOeqDEheQyQmTIGzCrMbX56e8y [HTTP/1.1 200 OK 16ms]
[14:02:28.826] removing attribute @ http://myurl/survey/6:27
[14:02:28.829] 0 @ http://myurl/survey/6:28
[14:02:28.833] GET http://myurl/survey/ajax?r_code=1&qid=9&other_text=& 
csrfmiddlewaretoken=m9s5BxbOeqDEheQyQmTIGzCrMbX56e8y [HTTP/1.1 200 OK 13ms]

因此,似乎正在进行两个 AJAX 调用,尽管我不确定为什么。有人可以向我解释这种行为或显示我哪里出错了吗?任何反馈将不胜感激!

4

0 回答 0