0

请参阅下面的代码以在单击按钮时在两个功能之间切换。但是按钮在单击后变得不可见,并且总是调用第二个函数。为什么!?

        $('#btnClick').click(function () {
               $(this).toggle(
                    //$('tr').toggleClass("highlight");
                    function () {
                        alert("Function one called");
                    },
                    function () {
                        alert("second function called");
                });
        });

谢谢!

4

4 回答 4

1

Thats because toggle(function,function...) is removed in 1.9+ what you should do is something like this

var tog = false;
$('#btnClick').click(function () {
    tog = !tog;
    if (tog) {
        alert("Function one called");
    } else {
        alert("second function called");
    }
});
于 2013-09-26T12:06:57.770 回答
1

如果您愿意,可以不使用全局变量

$('#btnClick').click(function () {
    var clicked = $(this).data('clicked') || 0;
    if (clicked % 2 == 0) {
        alert("Function one called");
    } else {
        alert("second function called");
    }
    $(this).data('clicked', clicked + 1);
});
于 2013-09-26T12:09:55.890 回答
0

现场演示

尝试使用 If 条件,你会得到要求

HTML:

<input type="button" id="btnClick" value="button 1"/>
<input type="button" id="btnClick1" value="button 2"/>

查询:

var i=true;
$('#btnClick').click(function () {
    i= !i;
    if (i) {
        alert("2nd function called");
        $('#btnClick1').toggle();
    } else {
        alert("1st function called");
        $('#btnClick1').toggle();
    }
});
于 2013-09-26T12:29:13.853 回答
-1
$('#btnClick').toggle(function () {
                        alert("Function one called");// work on first click
                    },
                    function () {
                        alert("second function called");// work on second click

        });

reference toggle

update for new version of jquery 1.9 + toggle is removed in new version

var stat= 0;
$('#btnClick').click(function () {
    stat= !stat;
    if (stat) {
        alert("Function one called");
    } else {
        alert("second function called");
    }
});
于 2013-09-26T12:08:16.660 回答