javascript jquery 文档准备好 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4778246/
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
jquery document ready if statement
提问by keith
I have a bit of jquery that works which you click on a radio button it opens a div. However, on page load the radio button may already be clicked so I would like to have it be open if it is clicked. I would assume I would do the same think below but just change "click" to something else. I cannot figure out exactly what though.
我有一些 jquery 可以工作,你点击它打开一个 div 的单选按钮。但是,在页面加载时,单选按钮可能已被单击,因此如果单击它,我希望将其打开。我会假设我会在下面做同样的想法,但只是将“点击”更改为其他内容。我无法弄清楚到底是什么。
$(".static_class").click(function() {
    if ($(this).val() === "0") {
        $("#none").show("fast");
    } else {
        $("#none").hide("fast");
    }
});
Any ideas?
有任何想法吗?
回答by Kautiontape
Checking the radio button's state on page load should work:
在页面加载时检查单选按钮的状态应该可以工作:
$(document).ready(function() {
  if($(".static_class").val()==="0")
    $("#none").show("fast"); 
  else $("#none").hide("fast");
}
Preferably, you should put your ifstatement in a function, and just call that function on $(document).ready()and on $(".static_class").click()
最好,你应该把你if的语句中的函数,并调用该函数$(document).ready()和$(".static_class").click()
Also an option is to allow the server-side script to set the div to either display: noneif you would not check the radio button or display: blockif the radio button button would be checked. This would have the div either show or hide before the page is ready.
还有一个选项是允许服务器端脚本将 div 设置为display: none如果您不选中单选按钮或display: block选中单选按钮按钮。这将使 div 在页面准备好之前显示或隐藏。
回答by Harish
Use jquery trigger function trigger an event after second click that will do it!
使用 jquery 触发功能在第二次点击后触发一个事件即可!
回答by lepe
As the "value" attribute in the radio button doesn't change, you should use :checked selector instead:
由于单选按钮中的“value”属性没有改变,你应该使用 :checked 选择器:
$(function() {
  if($(".static_class").is(":checked")) {
    ...
  }
}
回答by Blender
Well, there is$(document).ready():
嗯,还有就是$(document).ready():
$(document).ready(function() {
  if ($(this).val() === "0") {
    $("#none").show("fast");
  } else {
    $("#none").hide("fast");
  }
});
Your question is a bit hard to understand. Could you please elaborate a bit more?
你的问题有点难以理解。你能再详细一点吗?
回答by user113716
Try just adding .click()to the end of your handler assignment to trigger it.
尝试添加.click()到处理程序分配的末尾以触发它。
$(".static_class").click(function() {
    if( $(this).val() === "0" ) 
        $("#none").show("fast"); 
    else 
        $("#none").hide("fast");
}).click();

