如何编写脚本以绑定和解除绑定以停止和启动click事件

时间:2018-10-23 07:37:58

标签: javascript jquery

如何绑定(启用单击事件)和解除绑定(停止单击事件)jQuery Accordian取决于条件。如果我从下拉框中选择值1,则我想启用第三条链接Accordian,如果我选择值2我想取消绑定第三手风琴怎么办?

http://jsfiddle.net/nh0kcuop/1/

脚本:

//On click any <h3> within the container
$('#container h3').click(function(e) {

//Close all <div> but the <div> right after the clicked <a>
$(e.target).next('div').siblings('div').slideUp('fast');

//Toggle open/close on the <div> after the <h3>, opening it if not open.
$(e.target).next('div').slideToggle('fast');

});



$('#status').on('change',function(){

if(   $(this).val() == 1 ){

$('.text').bind('click');

}

if(   $(this).val() == 2 ){

$('.text').unbind('click');

}

});

1 个答案:

答案 0 :(得分:0)

您好,您已更新小提琴,可以使用“ on”和“ on”或“ off”启用点击功能

//On click any <h3> within the container
$('#container h3').click(function(e) {

    //Close all <div> but the <div> right after the clicked <a>
    $(e.target).next('div').siblings('div').slideUp('fast');

    //Toggle open/close on the <div> after the <h3>, opening it if not open.
    $(e.target).next('div').slideToggle('fast');

});



$('#status').on('change',function(){
console.log("status changed :" + $(this).val());
if($(this).val() == 1 ){
$('#container h3').on('click',function(e) {
    //Close all <div> but the <div> right after the clicked <a>
    $(e.target).next('div').siblings('div').slideUp('fast');
    //Toggle open/close on the <div> after the <h3>, opening it if not open.
    $(e.target).next('div').slideToggle('fast'); 
});
}
if($(this).val() == 2 ){
$('#container h3').off('click');

}

});

请访问here