将文本框值分配给 Javascript 中的字符串数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16540039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:04:10  来源:igfitidea点击:

Assigning Text Box Values to a String Array in Javascript?

javascript

提问by gogo

Is there a way to assign each of the values I enter into multiple text boxes into a string array in JavaScript?

有没有办法将我在多个文本框中输入的每个值分配到 JavaScript 中的字符串数组中?

javascript

javascript

<script type="text/javascript">
  var ch[];

  function getAllValues() {
   alert('Entered');

   for(var i=0;i<5;i++) {
      ch.push(document.getElementsByName("src")[i]);
    };

 alert(ch);
}
</script>

home.jsp

主页.jsp

<form method="post" name="myform" onsubmit="getAllValues();">//
    <%
        for (int i = 0; i < 5; i++) {
    %>
    <input type="text" name="src"/ >
    <%
        }
    %>
    <input type="submit" value="submit">
</form>


var ch[];

Here there are 5 text boxes. I want to assign the values entered here into an array using JavaScript.

这里有 5 个文本框。我想使用 JavaScript 将此处输入的值分配到数组中。

Does anybody have any idea how to do this? I was stuck on this for 2 days.

有谁知道如何做到这一点?我被困在这个问题上 2 天。

采纳答案by Qvcool

for(i=0;i<5;i++) {
  ch.push(document.getElementsByName("src")[i]);
}

Edit

编辑

<% for(int i=0;i<5;i++) { %>      
  <input type="text" name="src" onBlur="srcBlur()" />
<% } %>

...

...

var src = new Array();

function srcBlur(this) {
  if(this == document.getElementsByName("src")[0]) src[0] = this.value;
  else if(this == document.getElementsByName("src")[1]) src[1] = this.value;
  else if(this == document.getElementsByName("src")[2]) src[2] = this.value;
  else if(this == document.getElementsByName("src")[3]) src[3] = this.value;

  if(src[0] && src[1] && src[2] && src[3] && src[4]) {
    for(i=0;i<5;i++) {
      ch.push(src[i]);
    }
  }
}

回答by Anoop Joshi

 <script>
 var str = "";
 function getAllValues() {
     var inputs = document.getElementsByTagName('input');
     for (i = 0; i < inputs.length; i++) {
         str += inputs[i].value + "  ";
     }
     alert(str);
 }
</script>

回答by Derek Henderson

First, to save yourself a headache, you might consider giving your inputs unique names/ids, like this:

首先,为了避免让自己头疼,您可以考虑为输入提供唯一的名称/ID,如下所示:

<% for (int i = 0; i < 5; i++) { %>
    <input type="text" name="src<%= i %>" id="src<%= i %>"/ >
<% } %>

Then your JavaScript would be something like this:

那么你的 JavaScript 将是这样的:

var inputs = document.querySelectorAll('input[type=text]'),
    inputLen = inputs.length,
    ch = [],
    i,
    updateVal = function () {
        var id = this.id.replace('src', '');
        ch[id] = inputs[id].value;
    };

for (i = 0; i < inputLen; i += 1) {
    ch[i] = inputs[i].value;
    inputs[i].onchange = updateVal;
}

And you can see in this demothat it works. It populates the charray with the initial (blank) values of the inputs and then updates the values every time you change an input.

你可以在这个演示中看到它的工作原理。它ch使用输入的初始(空白)值填充数组,然后在每次更改输入时更新这些值。

回答by ema

Give a class to your inputs, for example inp. Then you can write:

为您的输入指定一个类,例如 inp。然后你可以写:

var result = $('.inp').map(function(m, i){ return i.value;})

result will be the array of values

结果将是值数组

回答by Prashant16

You can do it using jquery

你可以使用 jquery 做到这一点

var arr = new Array();
    $("input[name='src']").each(function () {
        arr.push($(this).val());
    })

回答by omui

var stringArr = [];

for (i = 0; i < 6; i++) {
    var element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("name", "textbox_" + i);
    element.setAttribute("id", "textbox_" + i);
    element.onkeyup = function () {
        keyPressFunc(this.id)
    };
    document.body.appendChild(element);
    stringArr.push("");
}

function keyPressFunc(elem) {
    var index = elem.split("_")[1];
    stringArr[index] = document.getElementById(elem).value;
    alert(stringArr);
}