2011-01-18 35 views
0

你好,我有這個bizzare行爲,當我點擊一個按鈕響應發生2次,我不明白是怎麼回事?Bizzare行爲與單擊事件上的按鈕

這是JavaScript的代碼:

function main(){ 
//Creating the function that will be executed by the showhid. 
$("#showhid").click(function(event){ 
    if ($('#btcreator').is(":visible") === true){ 

     $(this).button({ 
      icons:{secondary:'ui-icon-arrowthick-1-w'}, 
      text:false}); 

     $(this).animate({left:'97%'}, 'fast'); 

     $('#btcreator').animate({width:'hide'}, 'fast'); 

     return; 
    }else if($('#btcreator').is(":visible") === false){ 

     $(this).button({ 
      icons:{secondary:'ui-icon-arrowthick-1-e'}, 
      text:false}); 

     $(this).animate({left:'522px'}, 'fast'); 

     $('#btcreator').animate({width:'show'}, 'fast'); 

     return; 
    } 
}); 


//Executing the button function on the createBt Button. 
$("#createBt").button(); 
//Creating the function that will create the batute. 
$("#createBt").click(function(event){ 
    if (getData('fatherbt') == 'none'){ 
     window.fileLoc="modules/appgen/devBt/"+$('#body').attr('key').substring(0, 5); 
     elMnt = window.operation.createElement('element'); 
     elMnt.setAttribute('name', 'attr'); 
     elMnt.setAttribute('id', 'idEl'); 
     elMnt.setAttribute('attrname', 'id'); 
     elMnt.setAttribute('vtype', 'str'); 
     elMnt.setAttribute('value', getData('fncnm')); 
     window.data_lst = [elMnt]; 
     elAttr = window.operation.createElement('element'); 
     elAttr.setAttribute('name', 'attr'); 
     elAttr.setAttribute('id', 'nameEl'); 
     elAttr.setAttribute('attrname', 'name'); 
     elAttr.setAttribute('vtype', 'str'); 
     elAttr.setAttribute('value', getData('fncName')); 
     window.data_lst.push(elAttr); 
     window.addEl = 'function'; 
     catcher('crtBt'); 
     alert(event.data); 
    } 
}); 
}} 

這是網頁的代碼...

<HTML><HEAD><LINK href="http://dev.karinapp.com/modules/appgen/css/batute.css" id="css_batute_4" rel="stylesheet" type="text/css"/><LINK href="http://dev.karinapp.com/modules/batute/css/main.css" id="css_batute_22" rel="stylesheet" type="text/css"/><SCRIPT id="script0" src="http://dev.karinapp.com/modules/general/scripts/jQuery.js" type="text/javascript"><!--empty--></SCRIPT><SCRIPT id="script1" src="http://dev.karinapp.com/modules/general/scripts/ui/jquery.ui.core.js" type="text/javascript"><!--empty--></SCRIPT><SCRIPT id="script2" src="http://dev.karinapp.com/modules/general/scripts/ui/jquery.ui.widget.js" type="text/javascript"><!--empty--></SCRIPT><SCRIPT id="script3" src="http://dev.karinapp.com/modules/general/scripts/ui/jquery.ui.button.js" type="text/javascript"><!--empty--></SCRIPT><SCRIPT id="script4" src="/modules/general/scripts/general.js" type="text/javascript"><!--empty--></SCRIPT><LINK href="http://dev.karinapp.com/modules/general/css/karinapp-style/jquery-ui-1.8.4.custom.css" id="link4" rel="stylesheet" type="text/css"/><SCRIPT id="script5" src="/modules/appgen/scripts/batute.js" type="text/javascript"><!--empty--></SCRIPT><SCRIPT id="script6">function __init__(){ 

main(); 


}</SCRIPT><SCRIPT id="pdloadScr" src="/modules/general/scripts/loadPage.js" type="text/javascript"><!--empty--></SCRIPT><SCRIPT id="pdLoader" type="text/javascript">window.onload = function(){postLoad(); 
__init__();}</SCRIPT><SCRIPT id="script9" src="/modules/general/scripts/Catcher.js" type="text/javascript"><!--empty--></SCRIPT></HEAD><BODY id="body" key="fb4d1c88bad8893b8ca3ee30b19bf4190d378552"><DIV father="body" id="btcreator"> 
      <!--Comment--> 

      <LABEL father="@btcreator" id="fncNm_lbl"><!--empty-->    Name: 
      </LABEL> 

      <LABEL father="@btcreator" id="fatherlbl"><!--empty-->Father: 
      </LABEL> 
      <INPUT father="@btcreator" id="fncnm" type="text"/> 
      <SELECT father="@btcreator" id="fatherbt"><!--Comment--><OPTION father="@fatherbt" id="fatherbt_Opt0" name="fatherbt_Opt0" value="none">No father</OPTION></SELECT> 

      <BUTTON father="@btcreator" id="createBt"><!--empty-->Create</BUTTON><SELECT father="@btcreator" id="fncName"><OPTION father="@fncName" id="fncName_Opt0" name="fncName_Opt0" value="default">Select one...</OPTION><OPTION father="@fncName" id="fncName_Opt1" name="fncName_Opt1" value="oper">Operation</OPTION><OPTION father="@fncName" id="fncName_Opt2" name="fncName_Opt2" value="dirls">List of directories</OPTION><OPTION father="@fncName" id="fncName_Opt3" name="fncName_Opt3" value="opnfl">Open File</OPTION><OPTION father="@fncName" id="fncName_Opt4" name="fncName_Opt4" value="delete">Delete File</OPTION><OPTION father="@fncName" id="fncName_Opt5" name="fncName_Opt5" value="compare">Compare</OPTION><OPTION father="@fncName" id="fncName_Opt6" name="fncName_Opt6" value="cp">Copy</OPTION><OPTION father="@fncName" id="fncName_Opt7" name="fncName_Opt7" value="get">Get</OPTION><OPTION father="@fncName" id="fncName_Opt8" name="fncName_Opt8" value="switch">Switch(If-Else)</OPTION><OPTION father="@fncName" id="fncName_Opt9" name="fncName_Opt9" value="upload">Upload</OPTION><OPTION father="@fncName" id="fncName_Opt10" name="fncName_Opt10" value="modify">Modify(Elements)</OPTION><OPTION father="@fncName" id="fncName_Opt11" name="fncName_Opt11" value="create">Create(Elements)</OPTION><OPTION father="@fncName" id="fncName_Opt12" name="fncName_Opt12" value="find">Select(SQL)</OPTION><OPTION father="@fncName" id="fncName_Opt13" name="fncName_Opt13" value="insert">Insert(SQL)</OPTION><OPTION father="@fncName" id="fncName_Opt14" name="fncName_Opt14" value="update">Update(SQL)</OPTION></SELECT><BUTTON father="@btcreator" id="addRpl" value="Add Reply"><!--empty-->Add Reply</BUTTON><LABEL father="@btcreator" id="fncNameLbl"><!--empty-->Function:</LABEL> 

      <DIV father="@btcreator" id="rplCrt"> 
       <!--Comment--> 
       <BUTTON father="@rplCrt" id="crtMdl" value="Create Model"><!--empty-->Create model</BUTTON> 
       <BUTTON father="@rplCrt" id="stcRpl" value="Add Static Reply"><!--empty-->Add static Reply</BUTTON> 
       <BUTTON father="@rplCrt" id="addMdl"><!--empty--></BUTTON><TEXTAREA cols="45" father="@rplCrt" id="insertStc" rows="15">&lt;!--Comment--&gt;</TEXTAREA><TABLE father="@rplCrt" id="mdlLst" width="90%"><THEAD father="@mdlLst" id="thead0"><TR> 
<TH width="50%">Function name:</TH> 
<TH>Select</TH> 

</TR></THEAD><TBODY father="@mdlLst" id="tbody0"/></TABLE><LABEL father="@rplCrt" id="insertMdlLbl"><!--empty-->Insert model on:</LABEL><INPUT father="@rplCrt" id="mdIns"/> 
      </DIV> 
     <LABEL father="@btcreator" for="dtype" id="dTypelbl">Data-Type:</LABEL><SELECT father="@btcreator" id="dtype"><!--Comment--><OPTION father="@dtype" id="dtype_Opt0" name="dtype_Opt0" value="data">Data</OPTION><OPTION father="@dtype" id="dtype_Opt1" name="dtype_Opt1" value="int">Integer</OPTION><OPTION father="@dtype" id="dtype_Opt2" name="dtype_Opt2" value="float">Float</OPTION><OPTION father="@dtype" id="dtype_Opt3" name="dtype_Opt3" value="str">String</OPTION><OPTION father="@dtype" id="dtype_Opt4" name="dtype_Opt4" value="list">Array/List</OPTION></SELECT></DIV><BUTTON father="body" id="showhid"> 
       <!--Comment--> 
      </BUTTON></BODY></HTML> 

感謝。

+0

是否有可能您正在加載的腳本之一也在某處調用main()?該函數名稱是全局聲明的,所以任何人都可以調用它,並且它不是唯一的名稱。 –

+0

我已經改變了主要功能的名稱,並且相等。 – hidura

回答

1

如果在綁定事件之前取消綁定事件,則可以防止它們意外綁定兩次。所以,如果前行:

$( 「#showhid」)點擊(函數(事件){

你把這個:

$( '#showhid') .unbind( '點擊');

這行之前:

$( 「#createBt」)點擊(函數(事件){

你把這個:

$( '#createBt')解除綁定( '點擊')。

這應該可以解決您的問題。