单击按钮上的不触发

时间:2016-08-08 11:16:46

标签: javascript jquery html

我有一个html按钮,它连接到我页面顶部的JQuery函数,但点击似乎没有被触发?有什么我想念的吗?

$(function(){
                $('#ctl00_content_hidePast').click(function() {
                    var dt = new Date($.now() - 30 * 60000);
                    var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
                    $("td.bgtime").each(function() {
                        var bookingTime = ($(this).text().split(':'));
                        var d = new Date();
                        d.setHours(+bookingTime[0]);
                        d.setMinutes(bookingTime[1]);

                        var state = $("#ctl00_content_hdnPastBookingToggle").val();

                        if ($(d) > time) {
                            var timeRow = $(this).parent();
                            $(timeRow).toggle("slow",
                                function() {
                                    if (state.val === "0") {
                                        state.val = "1";} 
                                    else if (state.val === "1") {
                                        state.val = "0";
                                    };
                                });
                        };
                    });
                });
            });

按钮:

<button id="ctl00_content_hidePast"><i class='fa fa-eye-slash'></i> Hide/Show Past Bookings</button>

2 个答案:

答案 0 :(得分:2)

select min(sal) from (select sal from emp where rownum=<&n order by sal desc);

在这种情况下,您将事件绑定到文档。当您拥有动态dom元素时,通常会使用此选项。您无需将click事件绑定到文档。实际上,建议将它附加到非动态父级。例如,包含按钮的div。在这种情况下,代码将是:

$(function(){
                $(document).on('click','#ctl00_content_hidePast', function() {
                    var dt = new Date($.now() - 30 * 60000);
                    var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
                    $("td.bgtime").each(function() {
                        var bookingTime = ($(this).text().split(':'));
                        var d = new Date();
                        d.setHours(+bookingTime[0]);
                        d.setMinutes(bookingTime[1]);

                        var state = $("#ctl00_content_hdnPastBookingToggle").val();

                        if ($(d) > time) {
                            var timeRow = $(this).parent();
                            $(timeRow).toggle("slow",
                                function() {
                                    if (state.val === "0") {
                                        state.val = "1";} 
                                    else if (state.val === "1") {
                                        state.val = "0";
                                    };
                                });
                        };
                    });
                });
            });

它让事情更轻松。但由于我没有dom结构,所以我给了你一个无论如何都会有效的答案。

答案 1 :(得分:-1)

如果您使用动态按钮,则必须使用以下内容:

$(function(){
                $('#ctl00_content_hidePast').on("click", function() {
                    var dt = new Date($.now() - 30 * 60000);
                    var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
                    $("td.bgtime").each(function() {
                        var bookingTime = ($(this).text().split(':'));
                        var d = new Date();
                        d.setHours(+bookingTime[0]);
                        d.setMinutes(bookingTime[1]);

                        var state = $("#ctl00_content_hdnPastBookingToggle").val();

                        if ($(d) > time) {
                            var timeRow = $(this).parent();
                            $(timeRow).toggle("slow",
                                function() {
                                    if (state.val === "0") {
                                        state.val = "1";} 
                                    else if (state.val === "1") {
                                        state.val = "0";
                                    };
                                });
                        };
                    });
                });
            });

或(已弃用)

$(function(){
                $('#ctl00_content_hidePast').live("click", function() {
                    var dt = new Date($.now() - 30 * 60000);
                    var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
                    $("td.bgtime").each(function() {
                        var bookingTime = ($(this).text().split(':'));
                        var d = new Date();
                        d.setHours(+bookingTime[0]);
                        d.setMinutes(bookingTime[1]);

                        var state = $("#ctl00_content_hdnPastBookingToggle").val();

                        if ($(d) > time) {
                            var timeRow = $(this).parent();
                            $(timeRow).toggle("slow",
                                function() {
                                    if (state.val === "0") {
                                        state.val = "1";} 
                                    else if (state.val === "1") {
                                        state.val = "0";
                                    };
                                });
                        };
                    });
                });
            });