2011-11-18 38 views
1

這是一種簡單的(我知道),但我無法弄清楚。行動在單個事件上觸發三次

正在發生的事情:

我對其中一個按鈕觸發一個動作(其中刪除數據庫的特定記錄)一個jsp。但是我觀察到,按鈕被點擊時,動作會被觸發三次,即使記錄被刪除,也會引發錯誤消息。在jsp頁面上有兩個'div'(父子)(參見下面的代碼)。該按鈕在兒童之中。

我發現了什麼:

當我刪除父DIV(這使得一個孩子,父母)一切工作正常(的課程不同的是,父DIV正在執行的功能)。我讀這篇文章:

Event handler triggered twice instead of once

這表明 '事件冒泡' 的問題。我試着加入stopPropagation();但是那也沒有鍛鍊。

也許一些簡單的東西就是逃避我的眼睛。請幫忙。

這裏是我的代碼爲JSP:

<s:url id="scriptURL" action="getContactInfo" /> 
<sd:div href="%{scriptURL}" listenTopics="getContactInfo" formId="contactInfo" showLoadingText="false" preload="false"> 
    <s:url id="scriptURL1" action='delContactInfo'/> 
    <sd:div href="%{scriptURL1}" listenTopics="delContactInfo" formId="contactInfo" showLoadingText="false" preload="false"> 

     <s:actionerror/> 
     <s:actionmessage/> 
     <s:form name="contactInfo" id="contactInfo" action="editContactInfo"> 

      <sd:autocompleter id="contact" name="contact" label="Full Name " autoComplete="false" searchType="substring" list="contactList" valueNotifyTopics="getContactInfo"/> 
      <sd:autocompleter id="customer" name="customer" label="Company " autoComplete="false" searchType="substring" list="customerList" valueNotifyTopics="getContactInfo"/> 
      //////other controls//////// 


      //////other controls//////// 

      <s:submit id="submit" name="submit" value="Save Changes" align="center" onclick="saveEvent(event);"/> 

     </s:form> 

     <s:submit id="del" name="del" align="center" value="Delete" onclick="deleteEvent(event);"/> 

    </sd:div> 

</sd:div> 

的JS文件:

function deleteEvent(e) 
{ 
    alert('Delete the selected account !!'); 
    alert('Are you sure?'); 
    dojo.event.topic.publish('delContactInfo'); 

    event = e || window.event; 
    if ('bubbles' in event) { // all browsers except IE before version 9 
     if (event.bubbles) { 
      event.stopPropagation(); 
      alert('1'); 
     }else { // Internet Explorer before version 9 
      event.cancelBubble = true; 
      alert('2'); 
     } 
    }else{ 
     event.cancelBubble = true; 
     alert('3'); 
    } 
} 

function saveEvent(e){ 
    alert('Do you want to save the changes you made?'); 

    event = e || window.event; 
    if ('bubbles' in event) { // all browsers except IE before version 9 
     if (event.bubbles) { 
      event.stopPropagation(); 
     }else { // Internet Explorer before version 9 
      event.cancelBubble = true; 
     } 
    }else{ 
     event.cancelBubble = true; 
    } 
} 

我想這個問題就出在這裏只某處。因爲在這個行動之後被稱爲哪個在做它的工作。它只是被稱爲三次。但如果您覺得需要更多信息,請發表評論。

謝謝!

編輯

+0

沒有其他答案? – kanishk

回答

1

如果你必須這樣做在線使用這樣的事情:

<prior stuff here>; var e = arguments[0] || window.event; e.stopPropagation(); 

最好就是把代碼中的外部函數,並調用它像這樣:

onclick="handleEvent(event);" 


function handleEvent(e) 
{ 

    // function code 

    // then 
    event = e || window.event; 
    if ('bubbles' in event) { // all browsers except IE before version 9 
    if (event.bubbles) { 
     event.stopPropagation(); 
    } 
    else { // Internet Explorer before version 9 
     event.cancelBubble = true; 
    }  
    } 
} 

取自各地的示例http://help.dottoro.com/ljgfjsxd.php


爲了您的最新代碼,我建議這樣的:

<s:form name="contactInfo" id="contactInfo" action="editContactInfo"> 

     <sd:autocompleter id="contact" name="contact" label="Full Name " autoComplete="false" searchType="substring" list="contactList" valueNotifyTopics="getContactInfo"/> 
     <sd:autocompleter id="customer" name="customer" label="Company " autoComplete="false" searchType="substring" list="customerList" valueNotifyTopics="getContactInfo"/> 
     //////other controls//////// 


     //////other controls//////// 

     <s:submit id="submit" name="submit" value="Save Changes" align="center" onclick="saveEvent(event);"/> 
     <s:submit id="del" name="del" align="center" value="Delete" onclick="deleteEvent(event);"/> 
    </s:form> 

注:這兩個提交按鈕都以相同的形式。

+0

嗨,我剛纔試過這個。問題依然存在。任何更多的建議。 – kanishk

+0

@ kanishk查看編輯 – Hogan

+0

嗨,感謝您的支持,它仍然無法正常工作。我嘗試了一些修改,從Google獲得一些意見。但它只有一個區別,3個動作執行線程現在正在一個接一個地執行,而不是像前面那樣同時執行。 – kanishk

0

。請參閱下面的鏈接並使用由struts2提供的inbuild標籤。

How to restrict double click on button in struts /Java?

+0

此鏈接討論如何否定效果點擊按鈕兩次我的問題是,只有一次點擊導致行動執行3次。還要注意,當我刪除一個div一切正常......正如我在我的問題中提到的。 – kanishk