jQuery结合了$(document).ready和$('DropDown').change声明 [英] JQuery combine $(document).ready and $('DropDown').change declaration

查看:68
本文介绍了jQuery结合了$(document).ready和$('DropDown').change声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在事件$(document).ready 上运行函数function myFunction(),如果某个下拉列表发生变化,$('#myDropDown').change.有没有结合此声明的好方法,因此我没有两个单独的myFunction()调用?

I would like to run a function, function myFunction(), on the event $(document).ready and if a certain drop down changes, $('#myDropDown').change. Is there a good way to combine this declaration so I don't have two separate calls to 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();  //<---

    });

};

推荐答案

$(function () {
    $('#myDropDown').on('change', myFunction).trigger('change');
});

这篇关于jQuery结合了$(document).ready和$('DropDown').change声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆