使用Javascript按名称将多个元素添加到JSON对象

时间:2016-02-10 05:01:54

标签: javascript json

我有一个函数,例如,它接收10个值为值的文本框,并将它们放入一个JSON字符串中,然后存储在cookie中。我没有问题,如果我硬编码我抓住元素"赋值[]"的问题,但我也想添加其他文本框值,说& #34; quizzes []",作为一个例子,为了有一个长字符串,我将转换为JSON字符串。

[{"name":"assignments0","score":"1"},{"name":"assignments1","score":"2"},
{"name":"assignments2","score":"3"},{"name":"assignments3","score":"4"}] 

上面的代码适用于" name []"文本框,但我希望能够将其他元素添加到同一个JSON字符串。

我目前的输出结果如下:

[{"name":"assignments0","score":"22"},{"name":"assignments1","score":"19"},
{"name":"assignments2","score":"9"},{"name":"assignments3","score":"20"},
{"name":"quizzes0","score":"5"},{"name":"quizzes1","score":"9"}] 

如果我能够通过我的函数添加不同的文本框数组,我的预期输出将如下所示:

import subprocess
subprocess.Popen(r'explorer /select,mypath')

非常感谢任何正确方向的帮助。

3 个答案:

答案 0 :(得分:2)

您可以使用带有属性选择器的querySelectorAll()来获取所有元素,例如



function setObject(name, score) {
  this.name = name;
  this.score = score;
}

function setCookie() {
  var els = document.querySelectorAll('input[name="assignments[]"],input[name="quizes[]"]');

  var arr = [];
  for (var i = 0; i < els.length; i++) {
    var setObj = new setObject(els[i].name.slice(0, -2) + i, els[i].value);
    arr.push(setObj);
  }

  result1.innerHTML = JSON.stringify(arr, null, 2);


  var arr = [].map.call(els, function(el) {
    return new setObject(el.name.slice(0, -2) + i, el.value);
  });
  result2.innerHTML = JSON.stringify(arr, null, 2);
}
setCookie();
&#13;
<input name="assignments[]" value="1" />
<input name="assignments[]" value="2" />
<input name="assignments[]" value="3" />
<input name="assignments[]" value="4" />

<input name="quizes[]" value="1" />
<input name="quizes[]" value="2" />
<input name="quizes[]" value="3" />
<input name="quizes[]" value="4" />

<pre id="result1"></pre>
<pre id="result2"></pre>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

只是让它成为一个功能?

function getElementsArr(elementsName) {
    var elements = document.getElementsByName(elementsName + "[]");

    var arr = [];
    for(var i=0; i < elements.length;i++)
    {
        var setObj = new setObject(elementsName + i, elementss[i].value);
        arr.push(setObj);
    }

    return arr;
}

function setCookie(elementNames)
{   
    var allElements = [];
    for(var i = 0; i < elementNames.length; i++) {
      allElements.push(getElementsArr(elementNames[i]));
    }

    document.cookie = JSON.stringify(allElements);
}

setCookie(['assignments', 'quizzes']);

答案 2 :(得分:1)

您可以将所有输入名称数组分配给数组并对其进行迭代,如下面的代码所示。

stringstream_test completed in 10449080 nanoseconds.
stoi_test         completed in 10437559 nanoseconds.
stringstream_test completed in 9074869 nanoseconds.
stoi_test         completed in 9895661 nanoseconds.
stringstream_test completed in 11516788 nanoseconds.
stoi_test         completed in 12266627 nanoseconds.
stringstream_test completed in 10017085 nanoseconds.
stoi_test         completed in 9468441 nanoseconds.
stringstream_test completed in 9957401 nanoseconds.
stoi_test         completed in 9483004 nanoseconds.
stringstream_test completed in 10069845 nanoseconds.
stoi_test         completed in 9657653 nanoseconds.
stringstream_test completed in 9568359 nanoseconds.
stoi_test         completed in 9162406 nanoseconds.
stringstream_test completed in 9868536 nanoseconds.
stoi_test         completed in 9166439 nanoseconds.
stringstream_test completed in 9059762 nanoseconds.
stoi_test         completed in 11558076 nanoseconds.
stringstream_test completed in 11673084 nanoseconds.
stoi_test         completed in 13432386 nanoseconds.
...

&#13;
&#13;
var inputs = ["assignments", "quizzes", "three", "four"];
&#13;
function setObject(name, score) {
  this.name = name;
  this.score = score;
}

function setCookie() {

  var inputs = ["assignments", "quizzes", "three", "four"];
  var arr = [];

  for (var i = 0; i < inputs.length; i++) {

    var cookieName = inputs[i];
    var cookieValue = document.getElementsByName(inputs[i] + '[]');

    for (var j = 0; j < cookieValue.length; j++) {
      var setObj = new setObject(cookieName + j, cookieValue[j].value);
      arr.push(setObj);
    }
    
  }

  result.innerHTML = JSON.stringify(arr, null, 2);
  
  //document.cookie = JSON.stringify(arr);

}
setCookie();
&#13;
&#13;
&#13;

<强>输出

<input name="assignments[]" value="1" />
<input name="assignments[]" value="2" />
<input name="assignments[]" value="3" />

<input name="quizzes[]" value="11" />
<input name="quizzes[]" value="22" />
<input name="quizzes[]" value="33" />

<input name="three[]" value="111" />
<input name="three[]" value="222" />
<input name="three[]" value="333" />

<input name="four[]" value="1111" />
<input name="four[]" value="2222" />
<input name="four[]" value="3333" />

<pre id="result"></pre>