Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带有多选字段的数组创建JavaScript对象_Javascript_Jquery_Html - Fatal编程技术网

使用带有多选字段的数组创建JavaScript对象

使用带有多选字段的数组创建JavaScript对象,javascript,jquery,html,Javascript,Jquery,Html,您好,我希望创建一个JavaScript对象来存储从某些字段捕获的值。我有动态字段,用户可以在其中向页面添加更多字段 我能够使用下面的代码捕获并存储对象中的字段 var attributes = document.getElementsByName("attribute[]"); var locations = document.getElementsByName("location[]"); var len = attributes.length; var data = [] for(var

您好,我希望创建一个JavaScript对象来存储从某些字段捕获的值。我有动态字段,用户可以在其中向页面添加更多字段

我能够使用下面的代码捕获并存储对象中的字段

var attributes = document.getElementsByName("attribute[]");
var locations = document.getElementsByName("location[]");

var len = attributes.length;
var data = []
for(var i = 0; i < len; i++){
   var element = {
     "Attribute": attributes[i].value,
     "Location": locations[i].value,
   };
   data.push(element);
 };
var attributes=document.getElementsByName(“attribute[]);
var locations=document.getElementsByName(“位置[]”);
var len=attributes.length;
var数据=[]
对于(变量i=0;i
最近,我不得不在动态字段中添加一个名为“方法”的
字段,允许用户在下拉列表中选择多个方法。我正在为如何获得每个“属性”所选方法的数组而苦苦挣扎


非常感谢您的帮助

您可以使用以下函数:

function extract(select) {
  var array = [];
  for (var i = 0; i < select.length; i++) {
    if (select.options[i].selected) array.push(select.options[i].value);
  }

  return array
}
函数提取(选择){
var数组=[];
对于(变量i=0;i
document.querySelector('button')。addEventListener('click',function(){
var attributes=document.getElementsByName(“属性[]”);
var locations=document.getElementsByName(“位置[]”);
var methods=document.getElementsByName(“方法[]”);
var len=attributes.length;
var数据=[]
对于(变量i=0;i

一个
两个

一个 两个


单击我
您可以使用以下功能:

function extract(select) {
  var array = [];
  for (var i = 0; i < select.length; i++) {
    if (select.options[i].selected) array.push(select.options[i].value);
  }

  return array
}
函数提取(选择){
var数组=[];
对于(变量i=0;i
document.querySelector('button')。addEventListener('click',function(){
var attributes=document.getElementsByName(“属性[]”);
var locations=document.getElementsByName(“位置[]”);
var methods=document.getElementsByName(“方法[]”);
var len=attributes.length;
var数据=[]
对于(变量i=0;i

一个
两个

一个 两个


单击我
假设您的
选择
元素具有名称属性
选项

var attributes = document.getElementsByName("attribute[]");
var locations = document.getElementsByName("location[]");
var options = document.getElementsByName("options[]"); //<--------

var len = attributes.length;
var data = [];
for(var i = 0; i < len; i++){
   var element = {
     "Attribute": attributes[i].value,
     // Grab the texts of the selected options:
     options: Array.from(options[i].querySelectorAll('option:checked'), 
                         option => option.textContent),
     "Location": locations[i].value,
   };
   data.push(element);
}

假设您的
select
元素具有名称属性
options

var attributes = document.getElementsByName("attribute[]");
var locations = document.getElementsByName("location[]");
var options = document.getElementsByName("options[]"); //<--------

var len = attributes.length;
var data = [];
for(var i = 0; i < len; i++){
   var element = {
     "Attribute": attributes[i].value,
     // Grab the texts of the selected options:
     options: Array.from(options[i].querySelectorAll('option:checked'), 
                         option => option.textContent),
     "Location": locations[i].value,
   };
   data.push(element);
}

您的意思是
select
元素具有
multiple
属性?您知道您可以将新对象(或数组)推入对象中,对吗?我们称之为“多维”对象/数组。这些东西拯救了编程人员的生命或者,您是在询问如何获取刚刚设置的数据?很抱歉,我有点搞不清楚你到底在问什么。你的HTML在哪里!!!我希望您在命名元素时不要使用名称中的
[]
@ScottMarcus,有一些服务器端语言使用此信息在服务器端创建数组。你是说
select
元素具有
multiple
属性?你知道你可以将新对象(或数组)推到对象中,对吗?我们称之为“多维”对象/数组。这些东西拯救了编程人员的生命或者,您是在询问如何获取刚刚设置的数据?很抱歉,我有点搞不清楚你到底在问什么。你的HTML在哪里!!!我希望您在命名元素时不要使用名称中的
[]
@ScottMarcus,有些服务器端语言使用此信息在服务器端创建阵列。感谢您的帮助!我也尝试过这个解决方案,效果很好。可以看到我需要做一些更改的地方。不客气;-)我看到您选择了其他答案和解决方案。谢谢您的帮助!我也尝试过这个解决方案,效果很好。可以看到我需要做一些更改的地方。不客气;-)我看到你选择了另一个答案&解决方案。