简体   繁体   English

分隔文本框的值并将其放在javascript中的数组上

[英]separate values of a textbox and place it on an array in javascript

I have a textbox in which a user its going to input a value which I want to take and do the following. 我有一个文本框,用户可以在其中输入要获取的值并执行以下操作。 I want to be able to separate each word and add something to the end of them. 我希望能够分隔每个单词并在它们的末尾添加一些内容。 so if they put 123. I want to separate it and make it 1.jpg, 2,jpg 3.jpg after they are separated i want to put it in an array to compare to another array and go from there. 因此,如果他们放置123。我想将其分离,并使其分离后成为1.jpg,2.jpg 3.jpg。我想将其放入一个数组中,以与另一个数组进行比较,然后从那里开始。 Here is what I got so far 这是我到目前为止所得到的

<input type="text" id="textfromuser" />
<script type = "text/javascript">
function validate(){
var lists = [];
var VAlidation = document.getElementById("textfromuser").value;
lists.push(VAlidation);
for(var i=0; i<lists.length; i++)
alert(lists[i]).split('.');

this part of the code was to show that the value in the textbox is split and placed in an array but its not working.. any ideas? 这部分代码是为了显示文本框中的值已拆分并放置在数组中,但不起作用。

I think you are confusing arrays and strings, the value you obtain from the input is a string and you're afterwards adding it to the lists array and iterating over that array. 我认为您在混淆数组和字符串,从输入中获取的值是一个字符串,然后将其添加到列表数组并遍历该数组。

May be this is what you were looking for 可能这就是您想要的

HTML HTML

<input type="text" id="textfromuser" />

Javascript 使用Javascript

function validate(){
  // empty array
  var ar = [];
  // obtain a string from the input "text" field, retrieved by the "id" attribute
  var VAlidation = document.getElementById("textfromuser").value;
  // split the string into a character array
  var lists = VAlidation.split('');
  // iterate over the character array
  for(var i=0; i<lists.length; i++){
    // create a string concatenating the array's element and '.jpg'
    var str = lists[i]+'.jpg';
    // add the string var to the array
    ar.push(str);
  }
  return ar;
}

I created a jsfiddle to test it quickly if you want a try http://jsfiddle.net/kpw23/2/ 我创建了一个jsfiddle来快速测试它,如果您想尝试一下http://jsfiddle.net/kpw23/2/

UPDATE UPDATE

To compare arrays there are many ways to accomplish it. 比较数组有多种方法可以完成。 A simple way to achieve this would be to serialize the arrays and compare the serialized strings: 实现此目的的一种简单方法是序列化数组并比较序列化的字符串:

// having two arrays
var list = ['1.jpg','2.jpg','3.jpg'];
var list2 = ['1.jpg','2.jpg','3.jpg'];
// "Serialize" the arrays, the join() method 
/// joins the elements of an array into a string, and returns the string
serlist = list.join();
serlist2 = list2.join();
// Compare both serialized arrays
if(serlist==serlist2){
 alert("Arrays are equal");
}  
else{
 alert("Arrays are not equal");
}

This method will only work if the arrays are sorted the same way and contain exactly the same entries in the same array positions. 仅当数组以相同方式排序并且在相同数组位置中包含完全相同的条目时,此方法才有效。

Your alerting the value of lists[i] , not lists[i].split('.') . 您警告的是lists[i]的值,而不是lists[i].split('.') Try: 尝试:

alert(lists[i].split('.'));

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

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