How do I select the parent form based on which submit button is clicked?

前端 未结 5 845
孤城傲影
孤城傲影 2020-12-02 07:57

I have a web page with 3 forms on it. Not nested, just one after the other (they are almost identical, just one hidden variable that\'s different). A user will only fill i

5条回答
  •  心在旅途
    2020-12-02 08:19

    Eileen: No, it is not var nameVal = form.inputname.val();. It should be either...

    in jQuery:

    // you can use IDs (easier)
    
    var nameVal =  $(form).find('#id').val();
    
    // or use the [name=Fieldname] to search for the field
    
    var nameVal =  $(form).find('[name=Fieldname]').val();
    

    Or in JavaScript:

    var nameVal = this.form.FieldName.value;
    

    Or a combination:

    var nameVal = $(this.form.FieldName).val();
    

    With jQuery, you could even loop through all of the inputs in the form:

    $(form).find('input, select, textarea').each(function(){
    
      var name = this.name;
      // OR
      var name = $(this).attr('name');
    
      var value = this.value;
      // OR
      var value = $(this).val();
      ....
      });
    

提交回复
热议问题