javascript 为什么我不能使用jquery触发radio的`change`事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13448220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I trigger the `change` event of radio using jquery?
提问by Hanfei Sun
The page is here:
页面在这里:
http://cistrome.org/cps/seqconfig?did=2693
http://cistrome.org/cps/seqconfig?did=2693
And the original js codes are below(this one works well):
原始 js 代码如下(这个很好用):
$(document).ready(function(){
$(".open_gene").on('change', function(event) {
$('#Gene_field').show();
});
$(".close_gene").on("change", function(event){
$("#Gene_field").hide();
});
});
So the .close_gene
has an event handler for change
. But when I want to trigger this event manually to hide the #Gene_field
, like this:
所以.close_gene
有一个事件处理程序change
。但是当我想手动触发此事件以隐藏 时#Gene_field
,如下所示:
>>> $('.close_gene').trigger("change")
In FireBugs, the returned value is:
在 FireBugs 中,返回值是:
[input#nolimit_radio.close_gene all]
But the #Gene_field
is not hidden..
但#Gene_field
不是隐藏..
I was wondering that why I can't trigger change
event which should already bind
to function(event){ $("#Gene_field").hide();}
. Does anyone have ideas about this? Thanks!
我想知道为什么我不能触发change
应该已经bind
到的事件 function(event){ $("#Gene_field").hide();}
。有没有人对此有想法?谢谢!
回答by rajukoyilandy
Try this:
试试这个:
$(".close_gene").click();
Its working fine for me in Firebug Console... :)
它在 Firebug 控制台中对我来说工作正常...... :)
Update:
更新:
This should also work, but will not change the state of radio button
这也应该有效,但不会改变单选按钮的状态
$(document).ready(function(){
$(document).delegate(".open_gene",'change', function(event) {
$('#Gene_field').show();
});
$(document).delegate(".close_gene", "change", function(event){
$("#Gene_field").hide();
});
});
$('.close_gene').trigger("change");