我有一个在 .change()
上运行的 jquery 代码。
但我想在 jquery .ready()
上运行相同的代码,但它不起作用。
这是我的代码:
jQuery('.nhp-opts-select-hide-below').change(function(){
var option = jQuery('option:selected', this);
if(option.data('show').length > 0 || option.data('hide').length > 0){
jQuery(option.data('show')).each(function(){
if(jQuery(this).closest('tr').is(':hidden')){
jQuery(this).closest('tr').fadeIn('fast');
}
});
jQuery(option.data('hide')).each(function(){
if(jQuery(this).closest('tr').is(':visible')){
jQuery(this).closest('tr').fadeOut('fast');
}
});
}else{
jQuery(option.data('show')).each(function(){
if(jQuery(this).closest('tr').is(':visible')){
jQuery(this).closest('tr').fadeOut('fast');
}
});
jQuery(option.data('hide')).each(function(){
if(jQuery(this).closest('tr').is(':hidden')){
jQuery(this).closest('tr').fadeIn('fast');
}
});
}
});
请告诉我如何在 jquery 上运行上面的代码?
最佳答案
只需调用 .change()
没有争论。将整个内容放入准备好的处理程序中,然后:
jQuery(function($) {
$('.nhp-opts-select-hide-below').change(function(){
// snip...
}).change(); // that's it
});
关于jQuery - 运行change()和ready()函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247279/