Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取用javascript提交的表单的实例_Javascript_Forms_Hidden Field_Onsubmit - Fatal编程技术网

获取用javascript提交的表单的实例

获取用javascript提交的表单的实例,javascript,forms,hidden-field,onsubmit,Javascript,Forms,Hidden Field,Onsubmit,在提交之前,我想获得提交表单的句柄 页面中可能有多个表单 我不知道表单名称/id 原因:在模板级提交表单之前,我想做一些修改。使用jQuery时,应该是这样的: $(function() { $('form').submit(function() { // the code goes here; // variable `this` is an instance of form alert($(this).className); }); }); $("form"

在提交之前,我想获得提交表单的句柄

  • 页面中可能有多个表单
  • 我不知道表单名称/id

  • 原因:在模板级提交表单之前,我想做一些修改。

    使用jQuery时,应该是这样的:

    $(function() {
      $('form').submit(function() {
        // the code goes here;
        // variable `this` is an instance of form
        alert($(this).className);
      });
    });
    
    $("form").submit(function(e) {
        console.log("Form ID that is being submit %s",$(this).attr("id"));
    });
    
    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
        }
      };
    }
    

    如果使用jQuery,您可以考虑执行以下操作:

    $(function() {
      $('form').submit(function() {
        // the code goes here;
        // variable `this` is an instance of form
        alert($(this).className);
      });
    });
    
    $("form").submit(function(e) {
        console.log("Form ID that is being submit %s",$(this).attr("id"));
    });
    
    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
        }
      };
    }
    

    在纯javascript中,您可以通过执行document.getElementsByTagName(“表单”)并循环获得的数组来实现类似的功能。

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

    $(function() {
      $('form').submit(function() {
        // the code goes here;
        // variable `this` is an instance of form
        alert($(this).className);
      });
    });
    
    $("form").submit(function(e) {
        console.log("Form ID that is being submit %s",$(this).attr("id"));
    });
    
    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
        }
      };
    }
    
    for(var i=0;i
    您是否在使用某种JS框架(jQuery、Prototype、MooTools)?