获取在 javascript 中提交的表单的实例

发布于 2024-09-07 11:54:56 字数 109 浏览 0 评论 0原文

我想在提交之前处理正在提交的表单。

  1. 页面中可能有多个表单
  2. 我不知道表单名称/ID

原因:我想在模板级别提交表单之前进行一些调整。

I want to get a handle on the form being submitted, before submitting.

  1. There might be more than one form in the page
  2. I do not know the form name/id

reason: I want to do some tweeking before the form is being submitted in the template level.

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

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

发布评论

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

评论(3

木落 2024-09-14 11:54:56

如果没有 jQuery,它会是这样的:

for (var i=0; i < document.forms.length; i++){
  document.forms[i].onSubmit = function(){
    // logic goes here;
    // document.forms[i] is the instance of form
    if (formIsHappy()){
      return true; //form submits
    }else{
      return false; //prevents the submit
    }
  };
}

Without jQuery it'd be somthing like this:

for (var i=0; i < document.forms.length; i++){
  document.forms[i].onSubmit = function(){
    // logic goes here;
    // document.forms[i] is the instance of form
    if (formIsHappy()){
      return true; //form submits
    }else{
      return false; //prevents the submit
    }
  };
}
相权↑美人 2024-09-14 11:54:56

如果使用 jQuery,它会是这样的:

$(function() {
  $('form').submit(function() {
    // the code goes here;
    // variable `this` is an instance of form
    alert($(this).className);
  });
});

With jQuery it'd be something like this:

$(function() {
  $('form').submit(function() {
    // the code goes here;
    // variable `this` is an instance of form
    alert($(this).className);
  });
});
雨轻弹 2024-09-14 11:54:56

如果你使用 jQuery,你可以考虑做这样的事情:

$("form").submit(function(e) {
    console.log("Form ID that is being submit %s",$(this).attr("id"));
});

在纯 javascript 中,你可以通过执行 document.getElementsByTagName("form") 并循环遍历你得到的数组来完成类似的事情。

If you use jQuery, you could look at doing something like this:

$("form").submit(function(e) {
    console.log("Form ID that is being submit %s",$(this).attr("id"));
});

In Pure javascript you could so something similar by doing a document.getElementsByTagName("form") and loop through the array you get.

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