简体   繁体   English

Jquery 更换无法正常工作

[英]Jquery replacewith not working correctly

So I have a table that contains a column with a button.所以我有一个表,其中包含一个带有按钮的列。 When this button is clicked, it toggles the class of the current table row and then replaces the button.单击此按钮时,它将切换当前表格行的 class,然后替换该按钮。

$(document).ready(function() {

    $(".checkOut").click(function() {
        var currentRow = $(this).closest("tr");
        $(currentRow).removeClass("checkedIn");
        $(currentRow).addClass("checkedOut");
        $(this).replaceWith('<button title="Check In" class="checkIn" value="true" name="check_in"><img alt="Check In" src="../images/check.png"></button>');
    } );

    $(".checkIn").click(function() {
        var currentRow = $(this).closest("tr");
        $(currentRow).removeClass("checkedOut");
        $(currentRow).addClass("checkedIn");
        $(this).replaceWith('<button title="Check Out" class="checkOut" value="true" name="check_out"><img alt="Check Out" src="../images/minus.png"></button>');
    } );

});

The initial click seems to work just fine.最初的点击似乎工作得很好。 However when I click to change the state back to its orignal, it does not seem to work.但是,当我单击将 state 更改回原来的状态时,它似乎不起作用。 I think it is a problem with replaceWith.我认为这是replaceWith的问题。 Any help would be most appreciated!非常感激任何的帮助!

Because you're adding the Check In button dynamically (when you click the Check Out button), your click event listener is not going to be attached to it.因为您正在动态添加 Check In 按钮(当您单击 Check Out 按钮时),所以您的 click 事件侦听器不会附加到它。 You could use live instead:您可以使用live代替:

$(document).ready(function() {
    $(".checkOut").live("click", function() {
        //Your event handler code
    });

    $(".checkIn").live("click", function() {
        //Your event handler code
    });
}

You will need to use live for both, because after the first replacement, a new .checkOut element is dynamically added to the page.两者都需要使用live ,因为在第一次替换之后,一个新的.checkOut元素会动态添加到页面中。

$(document).ready(function() {
    $(".checkOut").live('click', function() {
        var $this = $(this),
            $currentRow = $this.closest("tr");
        $currentRow
            .removeClass("checkedIn")
            .addClass("checkedOut");
        $this.replaceWith('<button title="Check In" class="checkIn" value="true" name="check_in"><img alt="Check In" src="../images/check.png"></button>');
    });

    $(".checkIn").live('click', function() {
        var $this = $(this),
            $currentRow = $this.closest("tr");
        $currentRow
            .removeClass("checkedOut")
            .addClass("checkedIn");
        $this.replaceWith('<button title="Check Out" class="checkOut" value="true" name="check_out"><img alt="Check Out" src="../images/minus.png"></button>');
    });
});

1. You have to use .live() to attach a handler to the event for all elements which match the current selector, now and in the future. 1. 您必须使用.live()将处理程序附加到与当前选择器匹配的所有元素的事件,现在和将来。

2. You were doing an unnecessary re-constructing of the variable currentRow . 2. 您对变量currentRow进行了不必要的重建。 I added a $ sign, so you know it's already a jQuery object, and not to re-construct it.我添加了一个$符号,所以您知道它已经是 jQuery object,而不是重新构建它。

In addition, I added code to cache the $currentRow and $this objects, so you won't have to lookup the DOM every time you want to manipulate them.此外,我添加了缓存$currentRow$this对象的代码,因此您不必每次想要操作它们时都查找 DOM。

Instead of replacing you can just change the attribute values which will preserve the event handlers you attached to buttons.您可以更改属性值,而不是替换,这将保留您附加到按钮的事件处理程序。

$(document).ready(function() {

    $(".checkOut").click(function() {
        $(this).closest("tr").removeClass("checkedIn").addClass("checkedOut");
        $(this)
         .attr({ title: "Check In", class: "checkIn", value: "true", name: "check_in" })
         .find("img").attr({ src: "../images/check.png", alt: "Check In" });
    } );

    $(".checkIn").click(function() {
        $(this).closest("tr").removeClass("checkedOut").addClass("checkedIn");
        $(this)
         .attr({ title: "Check Out", class: "checkOut", value: "true", name: "check_out" })
         .find("img").attr({ src: "../images/minus.png", alt: "Check Out" });
    } );

});

an alternative to existing answers would be one handler for both check in and check out:现有答案的替代方案是一个处理程序,用于签入和签出:

$(".checkIn, .checkOut").live('click', function() {
        var $this = $(this),
            $currentRow = $this.closest("tr"),
            checking = $this.hasClass("checkIn"),
            classToAdd = ckecking ? "checkedOut" : "checkedIn",
            classToRemove = ckecking ? "checkedIn" : "checkedOut",
            buttonTitle = checking ? "Check Out" : "Check In",
            buttonName = checking ? "check_out" : "check_in",
            imgSrc = checking ? "minus" : "check";

        $currentRow.removeClass(classToRemove) .addClass(classToAdd);
        $this.replaceWith('<button title="'+buttonTitle+'" class="'+classToAdd+'" value="true" name="'+buttonName+'"><img alt="'+buttonTitle+'" src="../images/'+imgSrc+'.png"></button>');

    } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM