2012-11-19 3 views
0

여기에 내가 가지고 시도하고 무슨 일이 :JQuery와 다음

  • 사람이 입력 필드
  • 그들의 키보드 ENTER 충돌에 텍스트를 입력, 그것은 추가 after() 기능을 사용하여 현재 입력 필드 AFTER 입력 필드는 다음 입력 필드 그래서 그들은 계속 입력이 이상을하고를 heres 내 코드

이상 작동하지 않는 수 초점을 맞추고 :

$('.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(); 
    } 
}); 

을 heres 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] ')하지 않아야합니다. – 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

관련 문제