2010-10-08 28 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