JQuery结合了$(document).ready和$('DropDown')。change declaration

时间:2014-04-10 14:54:12

标签: jquery

如果某个下拉列表更改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();  //<---

    });

};

2 个答案:

答案 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);

});