首页 > 解决方案 > 在 document.ready 上触发一个 jQuery 函数

问题描述

我的 .change() 函数中有一些 Javascript,并希望在 .ready() 函数的函数中应用相同的代码。有什么方法可以实现这一点,而无需将代码复制并粘贴到函数中?

$('.selabw').change(function(){

     let that = $(this);
     let value = that.parent().next().prev().find('select').val();
     console.log('"' + value + '"');

     if(value == ''){
       that.parent().next().find('input').removeClass('disabledBetrag').removeAttr('disabled');
     } else {
       that.parent().next().find('input').val('0,00');
       that.parent().next().find('input').attr('disabled', 'true').addClass('disabledBetrag');
     }

});



$(document).ready( function(){
// trigger $('.selabw').change(function(){}); ? 
}

标签: jquery

解决方案


你可以这样试试。

$('.selabw').trigger('change')

http://api.jquery.com/trigger/


推荐阅读