从javascript中的html选项标记获取所有名称值

从javascript中的html选项标记获取所有名称值,javascript,Javascript,我有以下html代码 <select> <option>Joe</option> <option>Buckey</option> <option>Elen</option> <option>Rimzy</option> </select> <button>Submit</button> 你知道我怎样才能做到这一点吗?提前谢谢 附言:我对

我有以下html代码

<select>
  <option>Joe</option>
  <option>Buckey</option>
  <option>Elen</option>
  <option>Rimzy</option>
</select>
<button>Submit</button>
你知道我怎样才能做到这一点吗?提前谢谢


附言:我对javascript非常陌生。

以下是解决方案。您必须遍历所有选项元素,获取每个元素的值并将其推入数组中

函数getValues{ var数组=document.getElementsByTagName'option'; var-arr=[]; 对于变量i=0;i提交以下是解决方案。您必须遍历所有选项元素,获取每个元素的值并将其推入数组中

函数getValues{ var数组=document.getElementsByTagName'option'; var-arr=[]; 对于变量i=0;i getElementsByTagName'option'返回一个元素数组

无法在元素数组中获取.value-只能在单个元素上调用它

如何为数组中的每个元素获取.value?

很接近了

getElementsByTagName'option'返回一个元素数组

无法在元素数组中获取.value-只能在单个元素上调用它


如何为数组中的每个元素获取.value?

假设ES6是您的一个选项,则以下操作将按您的意愿执行:

// using a named function:
function grabTextFrom() {

  // initialising the local variable using 'let',
  // converting the argument supplied to Array.from()
  // to convert the Array-like Object (in this case
  // a NodeList, or HTMLCollection, from
  // document.querySelectorAll()) into an Array:
  let texts = Array.from(
    document.querySelectorAll(

      // 'this' is passed automatically from the
      // later use of EventTarget.addEventListener(),
      // and the dataset.textFrom property value is
      // the value stored in the data-textfrom attribute:
      this.dataset.textfrom
    )

  // iterating over that Array of elements using
  // Array.prototype.map() along with an arrow function:
  ).map(

    // 'opt' is a reference to the current Array-element
    // from the Array of elements over which we're iterating;
    // and here we return the value property-value of that
    // current element:
    opt => opt.value
  );

  // logging to the console for demo purposes:
  console.log(texts);

  // returning to the calling context:
  return texts;
}

// finding the first element which matches the
// supplied CSS selector, and adding the
// grabTextFrom() function as the event-handler
// for the 'click' event (note the deliberate
// lack of parentheses in the function-name):
document.querySelector('#submit').addEventListener('click', grabTextFrom);
函数grabTextFrom{ 让text=Array.from document.querySelectorAllthis.dataset.textfrom 地图 opt=>opt.value ; 控制台。日志文本; 返回文本; } document.querySelector'submit'。addEventListener'click',grabTextFrom; 乔 巴基 埃伦 边缘的
提交假设ES6是您的一个选项,那么以下内容将按照您的意愿执行:

// using a named function:
function grabTextFrom() {

  // initialising the local variable using 'let',
  // converting the argument supplied to Array.from()
  // to convert the Array-like Object (in this case
  // a NodeList, or HTMLCollection, from
  // document.querySelectorAll()) into an Array:
  let texts = Array.from(
    document.querySelectorAll(

      // 'this' is passed automatically from the
      // later use of EventTarget.addEventListener(),
      // and the dataset.textFrom property value is
      // the value stored in the data-textfrom attribute:
      this.dataset.textfrom
    )

  // iterating over that Array of elements using
  // Array.prototype.map() along with an arrow function:
  ).map(

    // 'opt' is a reference to the current Array-element
    // from the Array of elements over which we're iterating;
    // and here we return the value property-value of that
    // current element:
    opt => opt.value
  );

  // logging to the console for demo purposes:
  console.log(texts);

  // returning to the calling context:
  return texts;
}

// finding the first element which matches the
// supplied CSS selector, and adding the
// grabTextFrom() function as the event-handler
// for the 'click' event (note the deliberate
// lack of parentheses in the function-name):
document.querySelector('#submit').addEventListener('click', grabTextFrom);
函数grabTextFrom{ 让text=Array.from document.querySelectorAllthis.dataset.textfrom 地图 opt=>opt.value ; 控制台。日志文本; 返回文本; } document.querySelector'submit'。addEventListener'click',grabTextFrom; 乔 巴基 埃伦 边缘的
提交此作业?getElementsByTagName返回元素的集合…而不是单个元素。您需要迭代该集合来创建数组。这是家庭作业吗?getElementsByTagName返回元素集合…而不是单个元素。您需要迭代该集合来创建阵列。这太棒了。Array.from.It的出色使用令人惊叹。Array.from的大量使用。这个答案让您满意吗?如果你愿意,你可以投票并标记为最佳答案:这个答案让你满意吗?如果需要,您可以将投票和标记为最佳答案: