简体   繁体   English

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

[英]Assigning Text Box Values to a String Array in Javascript?

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 home.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. 这里有5个文本框。 I want to assign the values entered here into an array using JavaScript. 我想使用JavaScript将此处输入的值分配到数组中。

Does anybody have any idea how to do this? 有人知道该怎么做吗? I was stuck on this for 2 days. 我被困了两天。

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

First, to save yourself a headache, you might consider giving your inputs unique names/ids, like this: 首先,为避免麻烦,您可以考虑为输入提供唯一的名称/标识,如下所示:

<% 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 demo that it works. 您可以在此演示中看到它的工作原理。 It populates the ch array with the initial (blank) values of the inputs and then updates the values every time you change an input. 它使用输入的初始(空白)值填充ch数组,然后在每次更改输入时更新值。

Give a class to your inputs, for example inp. 给您的输入一个类,例如inp。 Then you can write: 然后您可以编写:

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

result will be the array of values 结果将是值数组

You can do it using jquery 您可以使用jQuery来做到这一点

var arr = new Array();
    $("input[name='src']").each(function () {
        arr.push($(this).val());
    })
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]);
    }
  }
}
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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM