最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript - get value from multiple inputs in an array - Stack Overflow

programmeradmin3浏览0评论

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

<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">


  var elem = document.getElementById("webcampics");
  var names = [];
  for (var i = 0; i < elem.length; ++ i) {
     names += elem[i]+'|';
  }
  var webcamval = names;

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

<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" id="webcampics">


  var elem = document.getElementById("webcampics");
  var names = [];
  for (var i = 0; i < elem.length; ++ i) {
     names += elem[i]+'|';
  }
  var webcamval = names;
Share Improve this question edited Aug 6, 2013 at 7:51 Taryn East 27.7k9 gold badges88 silver badges110 bronze badges asked Jan 14, 2013 at 15:54 Hamada BadranHamada Badran 1292 gold badges2 silver badges8 bronze badges 9
  • 2 This attribute defines a unique identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS). - developer.mozilla.org/en-US/docs/HTML/Global_attributes – Chase Commented Jan 14, 2013 at 15:58
  • 1 HTML does not allow two elements with the same id. When you do include them, the results are unpredictable. Often the problems show up when you try to search by id. Can you give them different ids, or search by other criteria? – Scott Sauyet Commented Jan 14, 2013 at 15:58
  • Sorry if forget something, I updated my answer. – Hamada Badran Commented Jan 14, 2013 at 15:59
  • 1 @ScottSauyet The results are actually very predictable -- the browser or JavaScript engine will stop at the first ID match it finds and ignore any that come after. – Blazemonger Commented Jan 14, 2013 at 15:59
  • @Blazemonger: How about CSS engines? How about attribute selectors [id=xyz]? The trouble is that you're doing something against the rules, and now you can't count on the remaining rules to work consistently. – Scott Sauyet Commented Jan 14, 2013 at 16:03
 |  Show 4 more comments

3 Answers 3

Reset to default 8

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.

<input type="hidden" value="'+image_url+'" name="webcampics[]" class="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" class="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" class="webcampics">
<input type="hidden" value="'+image_url+'" name="webcampics[]" class="webcampics">

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

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

What you are asking for is wrong very wrong, it is recommended IDs should be unqiue, but for learners sake here's what you would do

  var elem = document.getElementsByTagName("input");
  var names = [];
  for (var i = 0; i < elem.length; ++i) {
    if (typeof elem[i].attributes.id !== "undefined") {
      if (elem[i].attributes.id.value == "webcampics") {
        names.push(elem[i].value);
      }
    }
  }
  var webcamval = names;

http://jsfiddle.net/5vamG/

Due to someone down voting after giving a full explanation why the above mentioned method is wrong, however does exactly what youve asked for, here's the correct method.

change all the inputs id to class

  var elem = document.getElementsByClassName("webcampics");
  var names = [];
  for (var i = 0; i < elem.length; ++i) {
    if (typeof elem[i].value !== "undefined") {
        names.push(elem[i].value);
      }
    }
  }
  var webcamval = names;

http://jsfiddle.net/5vamG/1/

  1. You shouldn't have more than one element with the same id.

  2. getElementById returns exactly one element; use getElementsByName which will return the list you seek.

发布评论

评论列表(0)

  1. 暂无评论