简体   繁体   English

为什么一个$(document).ready(function(){会阻止另一个运行?

[英]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. 我了解我应该能够运行,并且可以运行许多$(document).ready(function(){我喜欢,但是事实是,在我的页面上,我不能。虽然我感谢别人让我知道我应该能够做到这一点,我真的希望有人可以告诉我为什么其中一个功能阻止另一个功能运行。

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. 我在同一页面上运行两个单独的$(document).ready(function(){。一个用于选择搜索结果时更新下拉菜单;另一个用于在用户单击时调用日历功能在“日期”文本框中。日期函数可以正常工作,除非我调用名称搜索函数。如果确实调用了名称搜索函数,则日期函数根本就不会运行。我是一个php hack,并且真的知道几乎没有JavaScript。

The two functions are included in different "js" pages. 这两个函数包含在不同的“ js”页面中。 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 +) (根据亚历克斯的评论更正了以前的帖子,添加了10倍+

See Deli's post: http://api.jquery.com/ready/ 请参阅Deli的帖子: http//api.jquery.com/ready/

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

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