jquery how to get form element types, names and values

前端 未结 5 1654
失恋的感觉
失恋的感觉 2020-12-30 07:34

I know I can get the name/value relationship by using

$(#form).serializeArray();

But is there a way to get the whole enchilada, type, name

5条回答
  •  情歌与酒
    2020-12-30 08:00

    Use $("form :input")

    Per the docs:

    Description: Selects all input, textarea, select and button elements.

    Now to your question,

    there a way to get the whole enchilada, type, name and value with one call?

    If you simply want to loop through the items,

    $("form :input").each(function(index, elm){
      //Do something amazing...
    });
    

    But if you want to return some sort of structure, you can use .map()

    var items = $("form :input").map(function(index, elm) {
        return {name: elm.name, type:elm.type, value: $(elm).val()};
    });
    

    Or if you simply want to get the elements

    $("form :input").get()
    


    Example of this on jsfiddle

提交回复
热议问题