2012-11-19 98 views
0

這裏就是我想有發生:jQuery的下一個()剛剛添加的元素

  • 有人進入文本輸入字段
  • ,當他們擊中了他們的鍵盤上ENTER,它增加了一個使用after()功能當前輸入域後,輸入字段,則側重該領域的投入,使他們能夠繼續打字,這樣做一遍又一遍

繼承人我的代碼是不工作:

$('.table-todo input[type=text]').live('keypress', function (e) { 
    if (e.which == 13) 
    { 
     $parent = $(this).parent('.field');  
     $.get('projects-add-task.php?show_delete=yes', function (data) { $parent.after(data); }, 'html'); 
     $(this).next('input[type=text]').focus(); 
    } 
}); 

繼承人的projects-add-task.php內容:

<div class="field"> 
<input type="text" name="task[]" /> 
<a href="#" title="Add Task Below" class="todo-add"><img src="images/icon-todo-add.png" alt="" onmouseover="this.src='images/icon-todo-add-h.png'" onmouseout="this.src='images/icon-todo-add.png'" /></a> 
<?php if ($_GET['show_delete'] == 'yes') { ?> 
<a href="#" title="Delete This Task" class="todo-delete"><img src="images/icon-todo-delete.png" alt="" onmouseover="this.src='images/icon-todo-delete-h.png'" onmouseout="this.src='images/icon-todo-delete.png'" /></a> 
<?php } else { ?> 
<img src="images/icon-todo-delete-o.png" alt="" /> 
<?php } ?> 
<div style="clear: both;"></div> 
</div> 

它只是不注重這是通過$.get增加了輸入和我無法弄清楚如何解決它。它將下一個輸入字段添加到頁面中,但它沒有將其聚焦。

+0

你可以在http://jsfiddle.net/上重現問題並鏈接到它嗎?如果他們能夠自己運行代碼,人們更容易幫忙。 –

+0

不應該'$(this).next('input [type = text]')。focus();'是'$ parent.next('input [type = text]')。focus();'? – banzomaikaka

回答

2

你可以這樣說:

$parent = $(this).parent('.field');  
    $this = $(this); 
    $.get('projects-add-task.php?show_delete=yes', function (data) { 
     $parent.after(data); 
     $parent.next().children('input[type=text]').focus(); 
    }, 'html'); 

的問題是,當因爲你使用的是AJAX調用創建執行$(this).next('input[type=text]').focus();下一個輸入元素實際上並沒有建立。你也需要父母的下一個元素。

+1

您的代碼不起作用。我知道這個問題,我需要在調用next()函數之前將新元素設置爲「live」,但我不知道該怎麼做。 – scarhand

+0

已編輯。你正在檢查這個元素的下一個,但它應該是父母的下一個元素實際上... –

+0

另一個夢幻般的答案。非常感謝你sidharth! – scarhand

相關問題