按“回车”提交表格不起作用(js)

时间:2012-06-11 03:54:52

标签: javascript google-gadget

我正在使用谷歌小工具编写一个简短的脚本,并尝试插入到谷歌网站。 当用户按 Go 按钮时,会触发javascript函数并且一切正常,但当用户按 Enter 时,会出现错误:

Missing or malformed url parameter

我不确定这是由我的代码或Google小工具/网站造成的。

我的HTML代码:

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

javascript功能

function GotoURL(dl) { 

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

由于

2 个答案:

答案 0 :(得分:4)

您应该在表单上使用onsubmit事件而不是按钮的onclick事件:

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

确保GotoURL()函数返回false以阻止表单实际提交。

答案 1 :(得分:1)

您需要在onsubmit代码中添加form处理程序:<form onsubmit="GotoURL(this)">