gpt4 book ai didi

javascript - 我可以获得调用该函数的按钮,但不能从搜索触发的 if 语句中调用,但可以调用任何其他函数就好了

转载 作者:行者123 更新时间:2023-11-30 17:56:14 25 4
gpt4 key购买 nike

//这个函数在按钮而不是在搜索中起作用

function initialize2() {// google maps load function
var mapOptions = {
center: new google.maps.LatLng(33.319914, 44.304771),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
marker.setMap(map); // set marker
}
google.maps.event.addDomListener(window, 'load', initialize); // initializ map

//这个按钮工作正常

<div id="sscbc">
<div id="ssi"></div>
<div id="sscb">
<button onclick="initialize2()">Enter ID</button> // loads map with marker
</div>
</div>

//这行不通

//用于测试搜索值和根据结果调用函数的函数 除了我试图调用的函数之外,任何函数都可以在这里工作

<script type="text/javascript">
function doSearch() {
searchvalue = document.searchbox.searchterm.value
if (searchvalue == "1") {
initialize2();
}
else {
alert("no good");
}
}

</script>

//搜索表单

   <div id="searchwrapper">
<form name="searchbox">
<input type="text" class="searchbox" name="searchterm"/>
<input type="image" src="images/opacbox1.png" class="searchbox_submit" value="Search" ONCLICK="Javascript:doSearch()" />
</form>
</div>

//完整代码

   <!DOCTYPE html>


<head>


<title>
</title>


<link rel="stylesheet" type="text/css" href="style2.css">


<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=&sensor=true">

</script>

<script type="text/javascript">

var myLatlng = new google.maps.LatLng(33.319914, 44.304771);

var marker = new google.maps.Marker({map: map, position:myLatlng, title:"Hello World!"});

var map = null;

function initialize()
{

var mapOptions =
{

center: new google.maps.LatLng(33.319914, 44.304771),

zoom: 12,

mapTypeId: google.maps.MapTypeId.ROADMAP,

disableDefaultUI: true,


};


var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);


// marker.setMap(map);



}

function initialize2()
{

var mapOptions =
{

center: new google.maps.LatLng(33.319914, 44.304771),

zoom: 12,

mapTypeId: google.maps.MapTypeId.ROADMAP,

disableDefaultUI: true,

};

var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

marker.setMap(map);

}

google.maps.event.addDomListener(window, 'load', initialize);



</script>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<head>



<body>

<div id="map-canvas"/>

</div>

<button onclick="initialize2()">Enter ID</button>

<form name="searchbox">
<input type="text" class="searchbox" name="searchterm"/>
<input type="image" src="images/opacbox1.png" class="searchbox_submit" Id="imageButton" value="Search" />

<script type="text/javascript">

function doSearch(e)
{
var elem = document.getElementsById('imageButton')[0];

elem.addEventListener('click', doSearch);


e.preventDefault();


searchvalue = document.searchbox.searchterm.value

if (searchvalue == "1")
{
initialize2();
}
else
{
alert("no good");
}
}


</script>

</body>



</html>

//CSS

body

{
}

#map-canvas
{
height: 500px;
width: 500px;
margin: 50px 50px 50px 50px;
}

#searchwrapper
{
width:310px;
height:40px;
background-image:url('images/searchbox6.png');
background-repeat:no-repeat;
padding:0px;
margin:0px;
position:relative;
}

#searchwrapper form
{
display: inline;
}

.searchbox
{
border:0px;
background-color:transparent;
position:absolute;
top:4px;
left:9px;
width:256px;
height:28px;
color: #fff;
}

.searchbox_submit
{
border:0px; /*important*/
background-color:transparent; /*important*/
position:absolute; /*important*/
top:4px;
left:265px;
width:32px;
height:28px;
}

最佳答案

e.preventDefault();

还不能添加评论。

关于javascript - 我可以获得调用该函数的按钮,但不能从搜索触发的 if 语句中调用,但可以调用任何其他函数就好了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18005514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com