用jquery将逗号分隔的输入框值分割成数组,并进行循环

时间:2022-10-16 00:20:39

I have a hidden input box from which I'm retrieving the comma-separated text value (e.g. 'apple,banana,jam') using:

我有一个隐藏的输入框,从中检索逗号分隔的文本值(例如。苹果、香蕉、果酱)使用:

var searchTerms = $("#searchKeywords").val();

I want to split the values up into an array, and then loop through the array.

我想把值分割成一个数组,然后在数组中循环。

2 个解决方案

#1


64  

var array = $('#searchKeywords').val().split(",");

then

然后

$.each(array,function(i){
   alert(array[i]);
});

OR

for (i=0;i<array.length;i++){
     alert(array[i]);
}

#2


1  

var array = searchTerms.split(",");

for (var i in array){
     alert(array[i]);
}

#1


64  

var array = $('#searchKeywords').val().split(",");

then

然后

$.each(array,function(i){
   alert(array[i]);
});

OR

for (i=0;i<array.length;i++){
     alert(array[i]);
}

#2


1  

var array = searchTerms.split(",");

for (var i in array){
     alert(array[i]);
}