クリックで動的に作成されたテーブルの行を非表示
-
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");
});
});
});
私は今の行を非表示にする必要があり、私は、いくつかの名前にする.ATTRを.parentを様々な方法のすべての種類を試してみました。
解決
このようなあなたの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を使用することができます。
私は一緒に簡単な例を投げたが、私は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
});
このような何かを、選択した行を非表示にするには
まず、あなたのテーブルのid(#mytableのようなもの)を得た。
$("#cancel_row").live("click", function(){
$(this).$("#mytable tr").hide();
});
あなたを助け願っています。
所属していません StackOverflow