我有一个带有2个文本框和2个按钮和2个表单的页面来包装文本框和按钮对。当用户输入内容并单击或提交输入时,事件将被发送两次,而这只
<script language="javascript" type="text/javascript">
function grabKeyword(){
var search=document.getElementById('search_box_second').value;
search=encodeSearch(search);
document.forms['Form1'].Term.value = search;
document.forms['Form1'].submit();
}
function submitSecondSearch(e)
{
if(window.event)
keycode = window.event.keyCode; // IE
else
keycode = e.which;
if (keycode == 13)
{
grabKeyword();
}
else
{
return true;
}
}
</script>
<form action="/" style="" method="post" name="Form1" id="Form1">
<input id="search_box_second" class="search_results_input" type="text" onKeyDown="submitSecondSearch(event)" /></span>
<span style="float:right"><input type="image" src="images/bttn_search_result.png" value="submit" onClick="grabKeyword()" /></span>
<input type="hidden" name="page" value="Page" />
<input type="hidden" name="action" value="action" />
<input type="hidden" name="Term" value="" />
</form>
This is one form. i have another form in same page.
<script language="javascript" type="text/javascript">
function grabSearch(){
var search=document.getElementById('search_box').value;
search=encodeSearch(search);
document.forms['Form'].Term.value = search;
document.forms['Form'].submit();
}
function encodeSearch(term){
return term.replace(/[^A-Za-z]/g," ");
}
</script>
<script language="javascript" type="text/javascript">
function submitenter(e)
{
if(window.event)
keycode = window.event.keyCode; // IE
else
keycode = e.which;
if (keycode == 13)
{
grabSearch();
}
else
{
return true;
}
}
</script>
<form action="/" style="" method="post" name="Form" id="Form"><input id="search_box" type="text" value="Search" onblur="if (this.value == '') {this.value = 'Search';}" onfocus="if (this.value == 'Search') {this.value = '';}" onKeyDown="submitenter(event)" />
<div id="search_arrow"><input type="image" src="/images/search_arrow.png" width="4" height="8" value="submit" onClick="grabSearch()" /></div>
<input type="hidden" name="page" value="Page" />
<input type="hidden" name="action" value="action" />
<input type="hidden" name="Term" value="" />
</form>
答案 0 :(得分:6)
你应该试试
return false;
在.submit()之后; 这样就省略了图像输入元素的默认行为。
答案 1 :(得分:3)
<input type="image" src="images/bttn_search_result.png" value="submit" onClick="grabKeyword(); return false;" />