事件在IE onclick和onsubmit中触发两次

时间:2011-07-27 12:51:56

标签: javascript internet-explorer javascript-events

我有一个带有2个文本框和2个按钮和2个表单的页面来包装文本框和按钮对。当用户输入内容并单击或提交输入时,事件将被发送两次,而这只在IE中发生。基于触发集成层的事件,请求被发送两次。这不应该发生。我真的无法在javascript代码中看到请求被发送两次(我试图sysout请求但它只打印一次)。我真的看不到事件发生两次,我怎么能看到事件发生两次。

<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>

2 个答案:

答案 0 :(得分:6)

你应该试试

return false;

在.submit()之后; 这样就省略了图像输入元素的默认行为。

答案 1 :(得分:3)

<input type="image" src="images/bttn_search_result.png" value="submit" onClick="grabKeyword(); return false;" />