如果某个下拉列表更改function myFunction()
,我希望在事件$(document).ready
和上运行一个函数$('#myDropDown').change
。有没有一种很好的方法来组合这个声明,所以我没有两次单独调用myFunction()
?
目前我有以下内容 -
$(document).ready(function () {
//NOTE: I have other code in here that also needs to be called on $(document).ready
myFunction(); // <--- I would like to get rid of one of the myFunction() calls
$('#myDropDown').change(function() {
myFunction(); //<---
});
};
答案 0 :(得分:2)
$(function () {
$('#myDropDown').on('change', myFunction).trigger('change');
});
答案 1 :(得分:0)
你必须为这个功能写2个电话。这是一个很好的方法:
$(document).ready(function () {
//NOTE: I have other code in here that also needs to be called on $(document).ready
myFunction(); // <--- I would like to get rid of one of the myFunction() calls
$('#myDropDown').change(myFunction);
});