简体   繁体   English

jQuery函数在加载时被调用

[英]Jquery function is being called on load

I have some jquery which I want to run on load time but It doesn't seem to hit the load event. 我有一些我想在加载时运行的jQuery,但似乎没有达到加载事件。 Here is my jquery 这是我的jQuery

$(function () {
    $('.field-of-work').load(function () {
        var $wrapper = $(this).closest('.field-of-work-wrapper');

        $wrapper.find('.position').hide();
        $wrapper.find('.position').find('select').val('');

    });
});

Why would the load event not get hit? 为什么加载事件不会被触发?

Try to use ready() method: 尝试使用ready()方法:

$(document).ready(function(){
    $('.field-of-work select').load(function () {
        var $wrapper = $(this).closest('.field-of-work-wrapper');

        $wrapper.find('.position').hide();
        $wrapper.find('.position').find('select').val('');

    });
});

$(function() is an alias for $(document).ready(function(){}) and your load event is fired before the document ready event. $(function()$(document).ready(function(){})的别名,并且在文档就绪事件之前会触发load事件。

If you want your code inside the load listener to fire on page loading, you should put it outside of any $(function() or $(document).ready() like this : 如果您希望加载侦听器中的代码在页面加载时触发,则应将其放在任何$(function()$(document).ready()如下所示:

$('.field-of-work').on('load', function () {
    var $wrapper = $(this).closest('.field-of-work-wrapper');

    $wrapper.find('.position').hide();
    $wrapper.find('.position').find('select').val('');

});

$(function(){
   /* Your other code here */
});

try this : 尝试这个 :

 $(document).ready(function() {
    $('.field-of-work').load(function () {
        var $wrapper = $(this).closest('.field-of-work-wrapper');

        $wrapper.find('.position').hide();
        $wrapper.find('.position').find('select').val('');

    });
});

Do you use the Load function correctly? 您是否正确使用加载功能?

Load requires an "url" as a parameter and returns the HTML into the matched element. 加载需要使用“ url”作为参数,并将HTML返回到匹配的元素中。

http://api.jquery.com/load/ http://api.jquery.com/load/

Description: Load data from the server and place the returned HTML into the matched element.

load( url [, data ] [, complete ] )Returns: jQuery

Do you want to perform the actions inside your load method right after the page has been loaded? 您是否要在页面加载后立即在load方法中执行操作?

You may want to use "ready()" instead of it: 您可能要使用“ ready()”代替它:

$(document).ready(function() {

       var $wrapper = $(this).closest('.field-of-work-wrapper');

        $wrapper.find('.position').hide();
        $wrapper.find('.position').find('select').val('');
});

Or did you just want to add an Event Handler load to it (like 还是只想向其中添加事件处理程序加载(例如

.on( "load", handler ).

http://api.jquery.com/load-event/ http://api.jquery.com/load-event/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM