[英]Trigger isn't firing
Okay, So I have looked through all the other questions around trigger not working. 好的,所以我仔细研究了触发器不起作用的所有其他问题。 I think my question is a bit different from the others I believe.
我认为我的问题与我相信的其他问题有所不同。
I have this here listening for clicks on a div: 我在这里听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);
});
Notice at the bottom I try to trigger the event stateChanged
, in on document ready I run this function... 请注意,在底部,我尝试触发事件
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);});
}
});
}
notice in that function I have... 注意该功能我有...
$(depends[0]).bind("stateChanged", function(x){if(x) $(a).show(500);
else $(a).hide(500);});
But the function just is never called. 但是函数永远不会被调用。
Here's the html 这是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>
The signature of your function should take an event as first argument : 函数的签名应将事件作为第一个参数:
$(depends[0]).bind("stateChanged",
function(evt, x) { // <-- here add "evt" as first argument
if(x) $(a).show(500);
else $(a).hide(500);
});
So this here goes back to this question here: https://superuser.com/questions/527761/javascript-being-cached-in-chrome 因此,这里回到这里的问题: https : //superuser.com/questions/527761/javascript-being-cached-in-chrome
Sometime it does it, sometimes it doesn't. 有时它做到了,有时却没有。
So for anyone that wants to use a input field that depends on a switch..... 因此,对于任何想要使用依赖于开关的输入字段的人.....
PS. PS。 Requires jquery and bootstrap.
需要jquery和bootstrap。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.