JavaScript - get value from multiple inputs in an array

后端 未结 3 484
抹茶落季
抹茶落季 2020-12-30 16:08

I am trying to get the value from muliple inputs with the same id in an array. I already used the forum, but haven\'t find a solution for me.

Exmaple



        
3条回答
  •  -上瘾入骨i
    2020-12-30 16:53

    You shouldn't have elements with identical id's within the document. ID's have to be unique throughout your entire markup, by specification. If you do it anyways, methods like document.getElementById will only match the very first occurence for instance.

    Use a class instead of ids.

    
    
    
    
    
    var inputs = document.getElementsByClassName( 'webcampics' ),
        names  = [].map.call(inputs, function( input ) {
            return input.value;
        }).join( '|' );
    

    Demo: http://jsfiddle.net/QgJrq/

提交回复
热议问题