Split comma-separated input box values into array in jquery, and loop through it

后端 未结 3 2142
天涯浪人
天涯浪人 2020-12-25 11:32

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

var searchTerms = $(\"#se         


        
相关标签:
3条回答
  • 2020-12-25 12:22
    var array = searchTerms.split(",");
    
    for (var i in array){
         alert(array[i]);
    }
    
    0 讨论(0)
  • 2020-12-25 12:24
    var array = $('#searchKeywords').val().split(",");
    

    then

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

    OR

    for (i=0;i<array.length;i++){
         alert(array[i]);
    }
    
    0 讨论(0)
  • 2020-12-25 12:29

    use js split() method to create an array

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

    then loop through the array using jQuery.each() function. as the documentation says:

    In the case of an array, the callback is passed an array index and a corresponding array value each time

    $.each(keywords, function(i, keyword){
       console.log(keyword);
    });
    
    0 讨论(0)
提交回复
热议问题