简体   繁体   中英

appending a picture to div when button is clicked

What I'm trying to do is have users input to be added to the string and the picture of google streetview append to the div called map

I tried two different codes but neither worked.

$("#search").click(function(){
var citySearch = $("#city").val();
var map = $("map");
var streetView = "https://maps.googleapis.com/maps/api/streetview?size=400x400&location="+ citySearch + "";
map.append('<img src="'+ streetView +'">')

})

and also used this with the button being onclick myFunction and onsumbit myFunction

function myFunction(){
var citySearch = $("#city").val();
var map = $("map");
var streetView = "https://maps.googleapis.com/maps/api/streetview?size=400x400&location="+ citySearch + "";
map.append('<img src="'+ streetView +'">')

}

HTML

<input type="text" name="" id="city">
<button id="search" onclick="myFunction()">Submit</button>

<div id="map">

</div>

如果您打算在ID为“ map”的div内渲染图像,则您的jQuery选择器必须为#map ,而不是map

var map = $("#map");

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