나는 편집 가능한 것으로 만들고 싶은 몇 개의 TD 요소를 가지고 있으며, 그 다음에 버튼을 클릭하면 정적으로 되돌아갑니다. 문제는 이러한 요소를 앞뒤로 전환하는 것입니다. 가능한 한 가장 쉬운 방법으로이를 어떻게 수행합니까?이벤트 처리기가 필요할 때 클래스 문제 토글
편집 -> 입력 요소로 요소 바꾸기 -> 단추 정적 함수 처리기 -> 단추 클릭 -> 입력 내용을 정적 내용으로 바꾸기 -> 이벤트 처리기 추가를 클릭하면 재귀적인 것처럼 보입니다. 내가 가진 유일한 문제는 새로 생성 된 요소에 핸들러를 첨부하는 것입니다.
자바 스크립트 :
// how do I encapsulate this into its own function? Seems recursive.
$('.edit-me').click(function(){
// remove the other editable fields.
// For some reason the second toggle errors out. - Why?
makeEditable(this);
$(this).toggleClass('edit-me');
$(this).toggleClass('static-me');
});
// attach a handler to the newly created elements
$('.static-me').click(function(){
// this is not working because newly created items do not have the event associated to them.
console.log("HERE");
makeStatic(this);
$(this).toggleClass('edit-me');
$(this).toggleClass('static-me');
});
}
$('.delete-me').click(function(){
pair_delete(this);
});
function makeEditable(obj){
// replace the elements with an editable element.
$(obj).parent().children('td.editable').each(function(index, item){
$(item).html('<input class="form-control edit-mode editing" data-field="' + $(this).attr('data-field') + '" value="' + $(this).html() + '" >');
// toggle for event handling.
$(item).removeClass('editable');
$(item).addClass('edit-mode');
});
$('.editing').change(function(){
// create the update
pair_update(this);
});
}
function makeStatic(obj){
// makes the row static.
$(obj).parent().children('td.edit-mode').each(function(index, item){
// replace with input field.
$(item).html('<td class="editable" data-field="'+ $(item).attr('data-field') +'">' + $(item).find('.editing').val() + '</td>');
$(item).addClass('editable');
$(item).removeClass('edit-mode');
});
}
HTML :
<td class="editable" data-field="pair_name"><?=$pair['pair_name']?></td>
<td class="editable" data-field="email_name"><?=$pair['email_id']?></td>
<td class="editable" data-field="sent_event_id"><?=$pair['sent_event_id']?></td>
<td class="editable" data-field="minutes"><?=$pair['minutes']?></td>
<td class="edit-me">
<button type="submit" class="btn btn-primary">
<i class="glyphicon glyphicon-pencil icon-white"></i>
</button>
</td>
읽기 http://api.jquery.com/on/#direct-and-delegated-events – j08691