Hot to get all form elements values using jQuery?

后端 未结 9 1650
谎友^
谎友^ 2020-12-22 23:45

Here is the HTML code:



    HTML Form Builder         


        
相关标签:
9条回答
  • 2020-12-23 00:28

    jQuery has very helpful function called serialize.

    Demo: http://jsfiddle.net/55xnJ/2/

    //Just type:
    $("#preview_form").serialize();
    
    //to get result:
    single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1
    
    0 讨论(0)
  • 2020-12-23 00:28

    Try this for getting form input text value to JavaScript object...

    var fieldPair = {};
    $("#form :input").each(function() {
        if($(this).attr("name").length > 0) {
            fieldPair[$(this).attr("name")] = $(this).val();
        }
    });
    
    console.log(fieldPair);
    
    0 讨论(0)
  • 2020-12-23 00:33

    The answer already been accepted, I just write a short technique for the same purpose.

    var fieldPair = '';
    $(":input").each(function(){
    fieldPair += $(this).attr("name") + ':' + $(this).val() + ';';
    });
    
    console.log(fieldPair);
    
    0 讨论(0)
提交回复
热议问题