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

后端 未结 3 2157
天涯浪人
天涯浪人 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: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);
    });
    

提交回复
热议问题