0

現在、ボタンが押された後、動的に作成されたテーブル行を非表示にしようとしています。これまでのところ、動的関数の一部を処理することができました。

各動的行には[キャンセル]ボタンと[保存]ボタンがあり、これらに簡単に応答できました。私の問題は、実際には行自体を操作することです。

$(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などのさまざまなメソッドを試しました。

4

4 に答える 4

3

次のように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>');
    });
});

(これは元のコードの修正バージョンであるため、まだ少し面倒です。)

于 2009-05-19T23:16:43.950 に答える
2

jQuery 1.3.2以降の新しいjQueryライブを使用して、動的に作成されたアイテムのクリック機能を保持できます。

http://docs.jquery.com/Events/live

于 2009-05-19T22:58:06.157 に答える
0

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();

});

それがあなたを助けることを願っています。

于 2009-05-20T00:59:43.570 に答える
0

私は簡単な例をまとめましたが、私は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);
});

});

于 2009-05-19T23:20:43.817 に答える