在 AJAX 回调中取消绑定提交事件

发布于 2024-11-18 13:22:14 字数 989 浏览 3 评论 0原文

如果未选中复选框,我会发出警报并阻止表单提交(单击提交按钮或 Enter)并绑定 false。我需要在 AJAX 回调中取消绑定,但无法执行此操作。

HTML:

<form action="act.php" onsubmit="formSubmit()" id="TypeForm" name="TypeForm" method="POST">
<label>
<input type="radio" name="Type" value="1">
<span>Type 1</span>
</label>
<label>
<input type="radio" name="Type" value="2">
<span>Type 2</span>
</label>
<input type="submit" name="submit" id="submit" value="Next" />
</form>

Javascript/JQuery:

<script>
  function formSubmit() {
    var self=this;
    $(self).bind('submit', false);
    if ($('#TypeForm').find('input:radio:checked').length == 0) {
      alert('A selection is required.');
    } else {
        $.ajax({
          type: "POST",
          url: "add.php",
          success: function() {
            $(self).unbind();
          }
        });
    }
  }
</script>

提前感谢您的帮助!

If a checkbox is not checked, I alert and prevent form submission (either submit button click or Enter) with bind false. I need to unbind in the AJAX callback, but am unable to do so.

HTML:

<form action="act.php" onsubmit="formSubmit()" id="TypeForm" name="TypeForm" method="POST">
<label>
<input type="radio" name="Type" value="1">
<span>Type 1</span>
</label>
<label>
<input type="radio" name="Type" value="2">
<span>Type 2</span>
</label>
<input type="submit" name="submit" id="submit" value="Next" />
</form>

Javascript/JQuery:

<script>
  function formSubmit() {
    var self=this;
    $(self).bind('submit', false);
    if ($('#TypeForm').find('input:radio:checked').length == 0) {
      alert('A selection is required.');
    } else {
        $.ajax({
          type: "POST",
          url: "add.php",
          success: function() {
            $(self).unbind();
          }
        });
    }
  }
</script>

Thanks in advance for your help!

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

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

发布评论

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

评论(3

心奴独伤 2024-11-25 13:22:14

也许您忘记了事件名称 - $(self).unbind('submit');

Maybe you forgot event name - $(self).unbind('submit'); ?

温折酒 2024-11-25 13:22:14

您想在用户按 Enter 或单击提交按钮时停止正常提交表单吗?然后您需要阻止事件采取默认操作。试试这个:

function formSubmit(event) {
    var self=this;

    event.preventDefault();

    if ($('#TypeForm').find('input:radio:checked').length == 0) {
      alert('A selection is required.');
    } else {
        $.ajax({
          type: "POST",
          url: "add.php",
          success: function() {
            $(self).unbind();
          }
        });
    }
  }

编辑:正如Trevor提到的,您应该使用jQuery来绑定您的函数,以便正确设置事件以在不同的浏览器上工作。代码可以是:

$(function() {
  $('#TypeForm').submit(formSubmit);
});

但我会像这样重写您的代码(未测试):

<script>

  $(function() {
    $('#TypeForm').submit(function (event) {
      var self=this;

      event.preventDefault();

      if ($(this).find('input:radio:checked').length === 0) {
        alert('A selection is required.');
        return false;
      }

      $.ajax({
        type: "POST",
        url: "add.php",
        success: function (data) {
          alert('success, data: ' + data); // optional
          // no need for this: $(self).unbind();
        }
      });
    }
  });

</script>

并从 HTML 中删除 onsubmit="formSubmit()"

Do you want to stop the normal submission of a form when user hits Enter or clicks the submit button? Then you need to prevent the event from taking the default action. Try this:

function formSubmit(event) {
    var self=this;

    event.preventDefault();

    if ($('#TypeForm').find('input:radio:checked').length == 0) {
      alert('A selection is required.');
    } else {
        $.ajax({
          type: "POST",
          url: "add.php",
          success: function() {
            $(self).unbind();
          }
        });
    }
  }

Edit: And as Trevor mentions, you should use jQuery to bind your function so that the event is properly setup to work across different browsers. The code could be:

$(function() {
  $('#TypeForm').submit(formSubmit);
});

But I would rewrite your code like this (NOT tested):

<script>

  $(function() {
    $('#TypeForm').submit(function (event) {
      var self=this;

      event.preventDefault();

      if ($(this).find('input:radio:checked').length === 0) {
        alert('A selection is required.');
        return false;
      }

      $.ajax({
        type: "POST",
        url: "add.php",
        success: function (data) {
          alert('success, data: ' + data); // optional
          // no need for this: $(self).unbind();
        }
      });
    }
  });

</script>

And remove the onsubmit="formSubmit()" from your HTML.

兮颜 2024-11-25 13:22:14

首先,
var self=这个;

应该是

var self=$(this)

其次,
您必须将表单绑定到 jQuery 事件模型中的提交操作才能访问 $(this)。

例:

$(function() { // short hand document ready call
   $('form-name').submit(function(formSubmit());
}

还有,为什么要解绑呢?

Firstly,
var self=this;

Should be

var self=$(this)

Secondly,
You must bind the form to the submit action within the event model of jQuery in order to access $(this).

Example:

$(function() { // short hand document ready call
   $('form-name').submit(function(formSubmit());
}

Also, why do you want to unbind it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文