无限循环中验证并提交表单而不输入?

发布于 2024-11-26 03:27:57 字数 464 浏览 0 评论 0原文

我使用这个 jquery 代码有一个无限循环,我知道为什么,但我不知道如何解决这个问题:

<form id="submitme">
  <input value="" name="n1" id="n1" type="text"/>
  <input value="Send" type="button"/> 
</form>
<script>
  $('#submitme').bind( 'submit', function() {
       $.post( 'validate.php', 'value=' + $('#n1').val(), function (data) {
             if (data == "true")
                $('#submitme').submit();
       }); 
  });
</script>

I am having an infinite cycle using this jquery code, I know WHY but I dont know HOW to fix this:

<form id="submitme">
  <input value="" name="n1" id="n1" type="text"/>
  <input value="Send" type="button"/> 
</form>
<script>
  $('#submitme').bind( 'submit', function() {
       $.post( 'validate.php', 'value=' + $('#n1').val(), function (data) {
             if (data == "true")
                $('#submitme').submit();
       }); 
  });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虚拟世界 2024-12-03 03:27:57

jQuery.validate 插件可以解决这个问题,我强烈建议您使用it:

$('#submitme').validate({
    rules: {
        n1: {
            remote: {
                url: 'validate.php',
                type: 'post'
            }
        }
    }
});

但如果您不想使用它,另一种可能性是使用全局变量,如下所示:

$('#submitme').submit(function() {
    if (!$.formSubmitting) {
        var $form = $(this);
        $.post('validate.php', { value: $('#n1').val() }, function (data) {
            if (data == 'true') { 
                // set the global variable to true so that we don't enter
                // the infinite loop and directly submit the form
                $.formSubmitting = true;
                $form.submit();
            }
        }); 
        return false;
    }

    return true;                     
});

只是备注:您放置在表单内的按钮不是提交按钮,因此单击它不会触发 提交处理程序。您应该将其设为提交按钮:

<input value="Send" type="submit" /> 

The jQuery.validate plugin takes care of this and I would strongly recommend you using it:

$('#submitme').validate({
    rules: {
        n1: {
            remote: {
                url: 'validate.php',
                type: 'post'
            }
        }
    }
});

But if you don't want to use it another possibility is to use a global variable, like so:

$('#submitme').submit(function() {
    if (!$.formSubmitting) {
        var $form = $(this);
        $.post('validate.php', { value: $('#n1').val() }, function (data) {
            if (data == 'true') { 
                // set the global variable to true so that we don't enter
                // the infinite loop and directly submit the form
                $.formSubmitting = true;
                $form.submit();
            }
        }); 
        return false;
    }

    return true;                     
});

Just a remark: the button you have placed inside the form is not a submit button so clicking it will not trigger the submit handler. You should make it a submit button:

<input value="Send" type="submit" /> 
风渺 2024-12-03 03:27:57

我不是 jQuery 专家,但在 Prototype 中,当您为某个操作编写事件处理程序并且不停止默认操作时,它将在所有回调功能完成后执行。因此,通过简单地翻转 if-else 语句,您应该能够避免无限循环:

$('#submitme').bind( 'submit', function(event) {
    $.post( 'validate.php', 'value=' + $('#n1').val(), function (data) {
    if (data != "true")
        // if validation has failed, prevent default action (submit)
        event.preventDefault();
    });
    // if default action was not prevented it will be executed
})

I am not a jQuery expert, but in Prototype, when you write an event handler for an action and you don't stop the default action, than it will be executed after all of your callback functionality was done. So by simply flipping the if-else statement you should be able to avoid a infinite loop:

$('#submitme').bind( 'submit', function(event) {
    $.post( 'validate.php', 'value=' + $('#n1').val(), function (data) {
    if (data != "true")
        // if validation has failed, prevent default action (submit)
        event.preventDefault();
    });
    // if default action was not prevented it will be executed
})
还给你自由 2024-12-03 03:27:57

我找到了这个解决方案:

<form id="submitme">
  <input value="" name="n1" id="n1" type="text"/>
  <input value="Send" type="button"/> 
</form>
<script>
  $('#submitme').bind( 'submit', function() {
       if ($.data( $('#submitme' ), 'validated'))
           return true;
       $.post( 'validate.php', 'value=' + $('#n1').val(), function (data) {
             if (data == "true") {
                $.data( $('#submitme'), 'validated', true );
                $('#submitme').submit();
             }
       }); 
       return false;
  });
</script>

I found this solution:

<form id="submitme">
  <input value="" name="n1" id="n1" type="text"/>
  <input value="Send" type="button"/> 
</form>
<script>
  $('#submitme').bind( 'submit', function() {
       if ($.data( $('#submitme' ), 'validated'))
           return true;
       $.post( 'validate.php', 'value=' + $('#n1').val(), function (data) {
             if (data == "true") {
                $.data( $('#submitme'), 'validated', true );
                $('#submitme').submit();
             }
       }); 
       return false;
  });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文