Convert javascript array to string

前端 未结 14 912
逝去的感伤
逝去的感伤 2020-11-29 16:41

I\'m trying to iterate over a \"value\" list and convert it into a string. Here is the code:

var blkstr = $.each(value, function(idx2,val2) {                        


        
14条回答
  •  情歌与酒
    2020-11-29 17:21

    convert an array to a GET param string that can be appended to a url could be done as follows

    function encodeGet(array){
        return getParams = $.map(array , function(val,index) {                    
            var str = index + "=" + escape(val);
            return str;
       }).join("&");
    }
    

    call this function as

    var getStr = encodeGet({
        search:     $('input[name="search"]').val(),
        location:   $('input[name="location"]').val(),
        dod:        $('input[name="dod"]').val(),
        type:       $('input[name="type"]').val()
    });
    window.location = '/site/search?'+getStr;
    

    which will forward the user to the /site/search? page with the get params outlined in the array given to encodeGet.

提交回复
热议问题