开发者

why the page refresh a time?

开发者 https://www.devze.com 2023-03-27 22:30 出处:网络
<form method=\"get\"action=\"/\" onsubmit=\"Subm开发者_运维技巧itForm()\"> <input type=\"text\" title=\"\" value=\"\" name=\"q\" class=\"search-input\" id=\"gsearch\" />
<form method="get"  action="/" onsubmit="Subm开发者_运维技巧itForm()">
<input type="text" title="" value="" name="q" class="search-input" id="gsearch" />
<input type="submit" value="" name="sa" id="search-button"/>
</form>
<script type="text/javascript">
function SubmitForm(){
window.open("http://test.com/gsearch.php?cx=015214977:8tebxhu0mrk&cof=FORID:11&ie=GB2312&as_q="+document.getElementById('gsearch').value);
return false;
}

</script>

when i submit the button, the original page refresh a time. how to prevent it? thank you.


change your form onsubmit attribute and insert a return like this:

<form method="get"  action="/" onsubmit="return SubmitForm();">


You correctly return false from your SubmitForm method, but you're not capturing that response in the onsubmit handler of the form. Change it to this:

<form method="get"  action="/" onsubmit="return SubmitForm()">

Note the addition of the return keyword in the above.


You don't need to use JavaScript for this. This will work same way:

<form method="get" target="_blank" action="http://test.com/gsearch.php">
    <input type="hidden" name="cx" value="015214977:8tebxhu0mrk" />
    <input type="hidden" name="cof" value="FORID:11" />
    <input type="hidden" name="ie" value="GB2312" />
    <input type="text" title="" value="" name="as_q" class="search-input" id="gsearch" />
    <input type="submit" value="Submit" id="search-button"/>
</form>

JavaScript is all nice and good, but really no need to use it when plain HTML can do the same thing exactly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号