2010-10-08 43 views
4

我使用此处的代码http://www.korvus.com/blog/geek/making-the-tab-key-work-with-jeditable-fields/来获取工作在jeditable字段之间的选项卡,并且如果这些字段本身可以正常工作,不过,我需要将我的字段放在一个表中,并且唯一一次Tab键的作用是从最后一个字段到第一个字段,当然,我需要它从第一个字段到下一个字段,依此类推......在表格中的jeditable字段之间切换

$('div.edit').bind('keydown', function(evt) { 
if(evt.keyCode==9) { 
    $(this).find("input").blur(); 
    var nextBox=''; 

    if ($("div.edit").index(this) == ($("div.edit").length-1)) { 
      nextBox=$("div.edit:first");   //last box, go to first 
     } else { 
      nextBox=$(this).next("div.edit"); //Next box in line 
     } 
    $(nextBox).click(); //Go to assigned next box 
    return false;   //Suppress normal tab 
}; 
}); 

表的格式像这样提前

+0

看到http://stackoverflow.com/questions/885616/tab-key-with-jeditable-fields – 2011-09-01 14:14:42

回答

8

<table> 

<tr> 
    <td class='leftcolumn'> 
    <strong>Firstname:</strong> 
    </td> 
    <td> 
    <div class='edit' id='firstname'><?=$userdetail['firstname']?></div> 
    </td> 
</tr> 

<tr> 
    <td class='leftcolumn'> 
    <strong>Lastname:</strong> 
    </td> 
    <td> 
    <div class='edit' id='lastname'><?=$userdetail['lastname']?></div> 
    </td> 
</tr> 
</table> 

感谢我认为问题是,你的输入字段不是直接的兄弟姐妹给对方,因此“下一步()”失败。我认为这会工作:

$('div.edit').bind('keydown', function(evt) { 
if(evt.keyCode==9) { 
    var nextBox=''; 
    var currentBoxIndex=$("div.edit").index(this); 
    if (currentBoxIndex == ($("div.edit").length-1)) { 
      nextBox=$("div.edit:first");   //last box, go to first 
     } else { 
      nextBox=$("div.edit").eq(currentBoxIndex+1); //Next box in line 
     } 
    $(this).find("input").blur(); 
    $(nextBox).click(); //Go to assigned next box 
    return false;   //Suppress normal tab 
}; 
}); 
+0

奇妙的,谢谢! – matjkd 2011-09-30 13:59:54

+0

这对我有用!除了我需要一个调整...你能帮我在这里,@SylvanK? http://stackoverflow.com/questions/24935069/using-tabbing-in-jeditable-fields-in-datatables – user2847749 2014-07-24 13:27:38