How do I get values of input element with the same name as an javascript array?

后端 未结 3 1362
孤独总比滥情好
孤独总比滥情好 2020-12-16 05:51

I want to get values of the input element that those elements are under the same name, name=\"items[]\"
for example:

...
相关标签:
3条回答
  • 2020-12-16 06:15

    Or use jQuery's map function:

    $("div input[name='items[]']").map( function() { return $(this).val(); } ).get();
    
    0 讨论(0)
  • 2020-12-16 06:31
    var values = [];
    $("input[name='items[]']").each(function() {
        values.push($(this).val());
    });
    
    0 讨论(0)
  • 2020-12-16 06:34

    JavaScript only:

    var values = [];
    var fields = document.getElementsByName("items[]");
    for(var i = 0; i < fields.length; i++) {
        values.push(fields[i].value);
    }
    

    Warning, see: getElementsByName in IE7

    0 讨论(0)
提交回复
热议问题