简体   繁体   中英

Assign input textfield having Javascript Array Value to javascript Array Variable

I'm currently trying to populate some location data for google maps initialization, So to an HTML input field I assign it with an JavaScript array format so that I use it for google maps but all in vein. . . . Here is my Code

      <input id="a" name="a" value='["WAPDA Town, Lahore, Pakistan","31.4311985","74.26435820000006","1"],["Johar Town, Lahore, Pakistan","31.469693","74.27284610000004","2"],'>

then I further Call it in my script to initialize it for locations.

 var location = '[' + document.getElementById('a').value + ']';

but it is not giving my desire result as I know I'm doing wrong. but what is the proper way of initalizing it? Any Suggestion

Try (I remove last comma from your input)

 var loc = JSON.parse(`[${a.value}]`); console.log(loc);
 <input id="a" name="a" value='["WAPDA Town, Lahore, Pakistan","31.4311985","74.26435820000006","1"],["Johar Town, Lahore, Pakistan","31.469693","74.27284610000004","2"]'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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