简体   繁体   中英

Why would one $(document).ready(function() { prevent another from running?

I understand that I am SUPPOSED to be able to run and many $(document).ready(function() { as I like, but the fact is, on my page, I CAN NOT. While I appreciate people letting me know that I should be able to do that, I am really hoping that someone might be able to tell me why one of these functions is preventing the other from running.

I am running two separate $(document).ready(function() { on the same page. One is used to update a drop down meny when a search result is selected. The other is used to call a calendar function when a user clicks in the "date" text box. The date function works fine unless I call the name search function. If I do call the name search function, the Date function just doesn't run at all. I am a php hack, and really know almost nothing a javascript.

The two functions are included in different "js" pages. Here are the two functions:

<script language="JavaScript" src="<?php echo MAIN_BASE_PATH?>/includes/utils.js" type="text/javascript"></script>


<script type="text/javascript"> 

$(document).ready(function(){
jquery_date('date');
});
</script>



function jquery_date(name){
$("input[name='"+name+"']").datepicker({ dateFormat: 'mm-dd-yy'  });
}

<link rel="stylesheet" type="text/css" href="../jquery.autocomplete.css" />
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript" src="../jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
    $("#student_search").autocomplete("autocomplete_student.php", {
           selectFirst: true
    });

     $("#student_search").result(function(event, data, formatted) {

        $("#student_menu_<?php echo $field_name;?>").val(data[1]);
});
});
</script>

You could easily merge the two into one (after all your page is fully loaded at the time of the call).

The problem with multiple binds is if one of them fails with an error the next ones in chain won't be executed, is it possible for your code to generate such an error ? (corrected previous post based on Alex comment, 10x added a +)

See Deli's post: http://api.jquery.com/ready/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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