0

好的,所以我已经查看了有关触发器不起作用的所有其他问题。我认为我的问题与我认为的其他问题有些不同。

我在这里监听 div 的点击:

$(".switch").click(function(){
    var state = ( $(this).children('.b1').is('.btn-success') ? 0 : 1 );
    $(this).attr('data-state', state  );
    $(this).children('.b1').toggleClass('btn-success');
    $(this).children('.b0').toggleClass('btn-danger');
    $(this).trigger('stateChanged', state);
});

请注意,在底部我尝试触发事件stateChanged,在准备好文档时我运行此功能...

function data_depends(){
    var key_name = urlName();
    $(document).find('[data-depends]').each(function(index){
        var depends = $(this).data("depends").split(", ");
        if(depends != null)
            if(depends[1] == "state"){
                if($(depends[0]).data("state") == "0")
                    $(this).show(500);
                else
                    $(this).hide(500);
                var a = this;
                $(depends[0]).bind("stateChanged", 
                            function(x){if(x) $(a).show(500); 
                                        else $(a).hide(500);});
            }
    });
}

注意我有这个功能......

$(depends[0]).bind("stateChanged", function(x){if(x) $(a).show(500);
else $(a).hide(500);});

但是这个函数永远不会被调用。

这是html

<div class="control-group">
    <label class="control-label" for="sale">Outcome</label>
    <div class="controls">
        <div id="sale" class="btn-group switch" data-toggle="buttons-radio" data-state="0" data-persist>
            <button type="button" class="btn b1">Sale</button>
            <button type="button" class="btn b0 btn-danger">No Sale</button>
        </div>
    </div>
</div>
<div class="control-group" data-depends="#sale, state">
    <label class="control-label" for="reason">No sale reason</label>
    <div class="controls">
        <input id="reason" type="text" placeholder="No sale reason" data-persist ><br>
    </div>
</div>
4

2 回答 2

1

您的函数的签名应该将事件作为第一个参数:

$(depends[0]).bind("stateChanged", 
    function(evt, x) { // <-- here add "evt" as first argument
        if(x) $(a).show(500);
        else $(a).hide(500);
});
于 2013-01-03T22:28:59.827 回答
0

所以这里回到这个问题:https ://superuser.com/questions/527761/javascript-being-cached-in-chrome

有时它会这样做,有时它不会。

因此,对于任何想要使用依赖于开关的输入字段的人......

PS。需要 jquery 和引导程序。


javascript

$(".switch").click(function(){
    var state = ( $(this).children('.b1').is('.btn-success') ? 0 : 1 );
    $(this).attr('data-state', state  );
    $(this).children('.b1').toggleClass('btn-success');
    $(this).children('.b0').toggleClass('btn-danger');
    $(this).trigger('stateChanged', state);
    //$("#" + this.id).trigger('stateChanged', state);
});

function data_depends(){
    var key_name = urlName();
    $(document).find('[data-depends]').each(function(index){
        var depends = $(this).data("depends").split(", ");
        if(depends != null)
            if(depends[1] == "!state"){
                var a = this;
                $(depends[0]).bind("stateChanged", function(evt, x){if(!x) {$(a).show(500);}else{ $(a).hide(500);}});
            }else if(depends[1] == "!state"){
                var a = this;
                $(depends[0]).bind("stateChanged", function(evt, x){if(x) {$(a).show(500);}else{ $(a).hide(500);}});
            }

    });
}

$(document).ready(function(e) {
    data_depends();
    $(".switch").trigger('stateChanged', 0);
});

html

<div class="control-group">
    <label class="control-label" for="sale">Outcome</label>
    <div class="controls">
        <div id="sale" class="btn-group switch" data-toggle="buttons-radio" data-state="0" data-persist>
            <button type="button" class="btn b1">Sale</button>
            <button type="button" class="btn b0 btn-danger">No Sale</button>
        </div>
    </div>
</div>
<div class="control-group" data-depends="#sale, state">
    <label class="control-label" for="reason">No sale reason</label>
    <div class="controls">
        <input id="reason" type="text" placeholder="No sale reason" data-persist ><br>
    </div>
</div>
于 2013-01-03T22:22:06.647 回答