显示/隐藏,当选择框的变化或负载 [英] Toggle show/hide when select box changes or on load
本文介绍了显示/隐藏,当选择框的变化或负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选择框,依赖于该选择框将显示/隐藏某些信息.
I have a select box that dependant on the select will show/hide certain information.
这很好(请参见代码).
This works fine (see code).
<script>
jQuery('#options').on('change',function(){
if(jQuery(this).val()=='rpp'){
jQuery('.rpp_fields').show();
} else {
jQuery('.rpp_fields').hide();
}
if(jQuery(this).val()=='busway'){
jQuery('.busway_fields').show();
} else {
jQuery('.busway_fields').hide();
}
});
</script>
如果页面加载时选择了其中一项,我现在试图获取相同的代码来执行.
I am now trying to get the same code to execute if one of the items is selected on page load.
任何人都可以帮忙吗?
谢谢
约翰
推荐答案
1)将处理程序提取到命名方法.在处理程序中使用该方法,并在页面加载时调用它.
1) Extract the handler to a named method. Use that method in the handler, and call it on page load.
function something () {}
$(selector).on('event', something);
something();
2)或者,在事件绑定的末尾使用trigger('change').
2) Or, use trigger('change') on the end of the event binding.
$(selector).on('event', ....).trigger('event');
我建议第一个,这样您就不会创建不必要的事件.
I suggest the first one so you are not creating an unnecessary event.
这篇关于显示/隐藏,当选择框的变化或负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文