开发者

How to convert a jQuery one('click') event to load in the background on page load?

开发者 https://www.devze.com 2023-03-16 23:42 出处:网络
I\'ve currently got this jquery triggered to the first click event of my select list. When someone first clicks on the select list, the jQuery is fired and it takes a few seconds to complete and be r

I've currently got this jquery triggered to the first click event of my select list.

When someone first clicks on the select list, the jQuery is fired and it takes a few seconds to complete and be ready for the user.

How can I change the function so that it operates in the background while (or as soon as) the page has loaded? I want it to be ready for the user before they click the select/pulldown menu (#my_select).

$('#my_select').one('click',function(){ 
        var myList = $(this);
        //myList.addClass("disabled");
        $('#my_select option').each(function(){
        var option = $(this);
        option.attr("disabled","disabled");
            $.get('<?php echo get_bloginfo('template_directory') ?>/getStyle.php',{option: $(this).val()},function(response) 
            {
                if (response=='true'){option.attr("disabled","");}else{opti开发者_StackOverflowon.addClass("disabled");}
            });
        });
        //myList.attr("disabled","");
    });


$(document).ready(function(){
var myList = $(this);myList.attr("disabled","disabled");
            $.get
            (
                '<?php echo get_bloginfo('template_directory') ?>/getTemplates.php',function(response) 
                {
                    myList.attr("disabled","");
                }
            );

});

Use $(document).ready(); to execute script after the dom has fully loaded.

See http://api.jquery.com/ready/


Quick & dirty workaround as an alternative solution

$(document).ready(function () {
  $('#my_select').trigger('click').unbind('click');
});

That will trigger the click event for #my_select once the page loads and then unbind the event so it doesn't happen when someone actually 'click's' it.

The proper way is to put the whole thing in a doc.ready as a standalone function rather than on an event.

0

精彩评论

暂无评论...
验证码 换一张
取 消