2011-01-23 46 views
0

我有一个输入:我用来修改表格的文本,但我不明白为什么当我按下回车键时,该功能会执行2次。 我尝试了一切,以停止双重执行,没有任何东西。 这是JS的代码:在JS上执行事件的双重问题

$("#mdfTbl").unbind('keyup'); 

    $("#mdfTbl").keyup(function(event){ 
     if (event.keyCode == 13 && getData(this)!=''){ 
      if ($(this).parent().attr('id').split('.')[1].split('_')[1] == '0'){//This means if the position is 0 the position is in the id the number before the _td 
       $("#attrTbl").attr('col', '0'); 
       father = $(this).parent();//Searching the father. 
       $("body").append($(this)); 
       $(this).hide(); 
       if (exist(getData("attrTbl"), getData($(this))) == false){ 
        //If theres no duplicate... 
        td = document.createElement('TD');//Creating the new TD 
        $(td).attr('id', $("#tbody1").children().length+'.td_1');//Adding the ID. 
        $(father).parent().append(td);//Appending the td to the grand father AKA: the body of the table. 
        $(father).append(getData($(this).attr('id')));//Appendding the data. 
        $(td).append(this);//Appending the modifier to the new TD 
        $(this).attr('value', '');//Cleaning 
        $(this).focus();//Focus again. 
        $(this).show(); 
       }else{ 
        father.append(this); 
        $(this).show(); 
       } 
      }else{ 
       var newCont = parseInt($("#tbody1").children().length)+1; 
       //getting the td that will store the data. 
       var father = $(this).parent(); 
       //Appending the data to the TD. 
       $(father).append(getData(this)); 
       //This going to be the next td 
       var td = document.createElement('TD'); 
       $(td).attr('id', (newCont)+'_td.0'); 
       $(td).append(this); 
       var tr = document.createElement('TR'); 
       $("#tbody1").append(tr); 
       $(tr).attr('id', (newCont)+'_tr'); 
       $(tr).append(td); 
       $(this).attr('value', '');//Cleaning 
       $(this).show(); 
       $(this).focus();//Focus again. 

      } 
     } 
    }); 

这是HTML代码:

<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__(){ 

      mainBt(); 
     }</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="ca6161395761c5f830dc17d47b553183af77430e"> 
     <div father="body" id="btcreator" style="display: block; "> 
      <!--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" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text"><!--empty-->Create</span></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> 

     <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><button father="@btcreator" id="goto">Go to object...</button><button father="@btcreator" id="frmSrc">Add from source</button><button father="@btcreator" id="frmbt">Add from batute</button><button father="@btcreator" id="addsttc">Add static</button><button father="@btcreator" id="prevsb" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" role="button" aria-disabled="false" title=""><span class="ui-button-text"><!--Comment--></span><span class="ui-button-icon-secondary ui-icon ui-icon-arrowthick-1-w"></span></button><button father="@btcreator" id="gtson" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" role="button" aria-disabled="false" title=""><span class="ui-button-text"><!--Comment--></span><span class="ui-button-icon-secondary ui-icon ui-icon-arrowthick-1-s"></span></button><button father="@btcreator" id="nxtsb" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" role="button" aria-disabled="false" title=""><span class="ui-button-text"><!--Comment--></span><span class="ui-button-icon-secondary ui-icon ui-icon-arrowthick-1-e"></span></button><button father="@btcreator" id="fthsb" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" role="button" aria-disabled="false" title=""><span class="ui-button-text"><!--Comment--></span><span class="ui-button-icon-secondary ui-icon ui-icon-arrowthick-1-n"></span></button><button father="@btcreator" id="svBt" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Save object</span></button><div father="@btcreator" id="btattr"><!--Comment--> 

<table border="1" father="btattr" id="attrTbl" width="100%" col="0"> 
    <thead father="@attrTbl" id="thead1"> 
    <tr> 
     <th>Property</th><th>Value</th> 
    </tr> 
    </thead> 
    <tbody father="@attrTbl" id="tbody1"> 
    <tr id="tbody0"> 
     <td id="tbody.tr_0">sdfasdfasdf</td> 
     <td id="1.td_1">asdfasdfasdf</td> 
    </tr> 
    <tr id="2_tr"> 
     <td id="2_td.0">adsfasdfsadf</td> 
    </tr> 
    <tr id="3_tr"> 
     <td id="3_td.0"><input id="mdfTbl" style="width: 180px; display: inline-block; " type="text"></td> 
    </tr> 
    </tbody> 
    <colgroup> 
    <col father="@attrTbl" id="col0" width="50%"> 
    <col father="@attrTbl" id="col1" width="50%"> 
    </colgroup> 
</table> 
</div></div> 

<button father="body" id="showhid" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" role="button" aria-disabled="false" title=" 

      " style="left: 502px; "><span class="ui-button-text"> 
       <!--Comment--> 
      </span><span class="ui-button-icon-secondary ui-icon ui-icon-arrowthick-1-e"></span></button></body></html> 
+0

看着我,好像你已经在几个地方忘记了`var`。 – Pointy 2011-01-23 17:21:04

+0

另外,如果你打算发布代码,那么确保它可以被试图帮助你的人轻松读取将是有礼貌的。 – Pointy 2011-01-23 17:25:14

回答

0

尝试从“KEYUP”处理程序返回false当检测到用户按下“回车”键。