클릭에 동적으로 생성 된 테이블 행을 숨기고 있습니다
-
23-08-2019 - |
문제
버튼을 누른 후 현재 동적으로 생성 된 테이블 행을 숨기려고합니다. 지금까지 나는 동적 기능의 일부를 처리했습니다.
각 동적 행에는 "취소"및 "저장"버튼이 있으며, 이에 쉽게 응답했습니다. 내 문제는 실제로 행 자체로 작업하는 것입니다.
$(function() {
$(".add").click(function(){
// Just append to the table
$("table#bookmarks tr:first").after("<tr class='new' id='<?php echo rand(1, 9999); ?>'><td></td><td><b>URL:</b> <input type='text' id='newURL' /><br /><b>Title:</b> <input type='text' id='newTitle' /><br /><b>Description:</b><br /><textarea id='newDesc'></textarea></td><td><b>Tags:</b> <input type='text' id='newTags' /></td><td><a href='#' class='save'>Save</a><br /><a href='#' class='cancel'>Cancel</a></td></tr>");
$('span#links').html('<i style="color: #FF0000">You must reload to recount links!</i>');
// Actually, the user doesn't want to add another link
$('.cancel').click(function() {
$(this).parents(".new").animate({ backgroundColor: "#FF0000" }, "fast").animate({ opacity: "hide" }, "slow");
});
// Seems the user wants to add a link!
$('.save').click(function() {
$("table#bookmarks tr.new #id").animate({ backgroundColor: "#FFFFFF" }, "fast").animate({ opacity: "hide" }, "slow");
});
});
});
이제 행을 숨겨야합니다. 모든 종류의 다양한 방법, .parent, .attr을 시도했습니다.
해결책
jQuery 함수를 다음과 같이 연쇄 시도하십시오.
$(function() {
$(".add").click(function() {
$("<tr class='new'><td></td><td><b>URL:</b> <input type='text' id='newURL' /><br /><b>Title:</b> <input type='text' id='newTitle' /><br /><b>Description:</b><br /><textarea id='newDesc'></textarea></td><td><b>Tags:</b> <input type='text' id='newTags' /></td></tr>")
.append($("<td></td>")
.append($("<a href='#'>Save</a><br/>")
.click(function() { $(this).parents(".new").animate({ backgroundColor: "#FF0000" }, "fast").animate({ opacity: "hide" }, "slow"); }))
.append($("<a href='#'>Cancel</a>")
.click(function() { $(this).parents(".new").animate({ backgroundColor: "#FF0000" }, "fast").animate({ opacity: "hide" }, "slow"); })))
.insertAfter($("table#bookmarks tr:first"));
$('span#links').html('<i style="color: #FF0000">You must reload to recount links!</i>');
});
});
(이것은 원래 코드의 수정 된 버전이므로 여전히 약간 지저분합니다.)
다른 팁
jQuery 1.3.2 이후 새로운 jQuery Live를 사용하여 동적으로 생성 된 항목의 클릭 함수를 보존 할 수 있습니다.
나는 빠른 예를 함께 던졌지 만 jQuery에 약간의 녹슬 었다는 것을 인정할 것입니다. 그러나이 코드는 적어도 저에게는 효과가 있습니다.
$(function() {
$(".add").click(function(){
var save = $("<a href='#' class='save'>Save</a>").click(function() {
$(this).parent().animate({ backgroundColor: "#FFFFFF" }, "fast").animate({ opacity: "hide" }, "slow")
})
var cancel = $("<a href='#' class='cancel'>Cancel</a>").click(function() {
$(this).parent().animate({ backgroundColor: "#FFFFFF" }, "fast").animate({ opacity: "hide" }, "slow")
})
var buttonTD = $("<td></td>");
buttonTD.append(save);
buttonTD.append(cancel);
var row = $("<tr class='new' id='<?php echo rand(1, 9999); ?>'><td></td><td><b>URL:</b> <input type='text' id='newURL' /><br /><b>Title:</b> <input type='text' id='newTitle' /><br /><b>Description:</b><br /><textarea id='newDesc'></textarea></td><td><b>Tags:</b> <input type='text' id='newTags' /></td></tr>")
.append(buttonTD);
});
});
jQuery 라이브 기능 :
$("#sendmail").live("click", function(){
// your code goes here
});
다음은 내가 어떻게 사용했는지에 대한 예입니다.
$("#sendmail").live("click", function(){
$("#emailres").html('<img src="../images/ajax-loader.gif">');
var youremail = $("#youremail").val();
var subject = $("#subject").val();
var message = $("#message").val();
$.ajax({
type: 'post',
url: 'email.php',
data: 'youremail=' + youremail + '&subject=' + subject + '&message=' + message,
success: function(results) {
$('#emailres').html(results);
}
}); // end ajax
});
선택한 행을 숨기려면 다음과 같은 작업을 수행하십시오.
먼저 테이블을 신분증 (#MyTable과 같은 것)
$("#cancel_row").live("click", function(){
$(this).$("#mytable tr").hide();
});
그것이 당신에게 도움이되기를 바랍니다.
제휴하지 않습니다 StackOverflow