简体   繁体   中英

Press “enter” to submit form doesn't work (js)

i'm using google gadget to write a short script and try to insert to google site. When user press Go button, javascript function is triggered and everything works fine, but when user press Enter , error occurs:

Missing or malformed url parameter

I'm not sure it's caused by my code or google gadget/site.

My html code:

<form name="theform">
<input type="text" size="20" id="search"/> 
<INPUT TYPE="submit" VALUE="Go!" ONCLICK="GotoURL(this.form)">
</form>

javascript function

function GotoURL(dl) { 

var mySearch = document.getElementById("search").value;
url='some_url'+mySearch;
window.open(url,'_blank');
} 

Thanks

You should use the onsubmit event on the form instead of the onclick event of the button:

<form name="theform" onsubmit="GotoURL(this)">
    <input type="text" size="20" id="search"/> 
    <input type="submit" value="Go!">
</form>

Make sure that the GotoURL() function returns false to prevent the form from actually submitting.

您需要向form标签本身添加一个onsubmit处理程序: <form onsubmit="GotoURL(this)">

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