jQuery를 사용하여 AJAX로 항목을 편집하고 업데이트하려고합니다.jQuery()를 사용하여 목록을 AJAX로 업데이트하십시오.
HTML
<ul>
<li data-id="1">
<span class="title">Title 1</span><span><a href="#" class="changeLink">Change</a></span>
</li>
<li data-id="2">
<span class="title">Title 2</span><span><a href="#" class="changeLink">Change</a></span>
</li>
<li data-id="3">
<span class="title">Title 3</span><span><a href="#" class="changeLink">Change</a></span>
</li>
</ul>
jQuery를
$('a.changeLink').on('click', function(e)
{
e.preventDefault();
// get parent LI element
var li = $(this).closest('li');
// catch current title
var title = li.children('span.title').text();
// replace span
li.children('span.title').html('<input type="text" value="'+title+'" />');
// change the link class and label
$(this).removeClass('changeLink').addClass('saveLink').text('Save');
});
$('a.saveLink').on('click', function(e)
{
e.preventDefault();
// get parent LI element
var li = $(this).closest('li');
// get the id for the AJAX query
var id = li.attr('data-id');
// get the input value
var title = li.children('span.title input').val();
// restore span text with new
li.children('span.title').text(title);
// restore link
$(this).removeClass('saveLink').addClass('changeLink').text('Change');
// make AJAX post to save new title
$.post('updatetitle.php',{ 'id': id, 'title': title }, function(){ alert('OK'); });
});
내 문제는 saveLink 클릭 이벤트가 발생하지 않았다는 것 같다. 또한 changeLink 클릭 이벤트가 두 번 이상 발생하는 것 같습니다. 왜 이런 일이 발생합니까? 링크에 더 이상 changeLink
클래스가 없습니다.
나는이 여기 jsFiddle에 넣어 : http://jsfiddle.net/jtheman/SZk4h/1/
다행 내가 어떤 입력을 얻을 수 있다면, 내 오류가 무엇입니까?
위대한, 내 첫 번째 문제를 해결합니다. 그러나 changeLink 이벤트가 링크를 한번 이상 클릭하면 왜 발생합니까? 이것을보십시오 : http://jsfiddle.net/jtheman/SZk4h/10/ – jtheman
또한 title = li.children ('span.title input') .val(); undefined를 반환합니다 - 왜? – jtheman
@jtheman .. 업데이트 된 게시물 확인 –