javascript ASP.NET - jQuery (document).ready() 函数不会被用户控制触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28215250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:37:03  来源:igfitidea点击:

ASP.NET - jQuery (document).ready() function is not fired for user control

javascriptc#jqueryasp.net

提问by Sormita Chakraborty

I have the following jQuery function in my ASP.net user control:

我的 ASP.net 用户控件中有以下 jQuery 函数:

<head>
<title></title>
<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript" src="~/Scripts/jquery-1.4.1.js">

    $(document).ready(function () {
        alert("In Jquery");
        $("[id*=RadioButtonListYesNo]").change(function () {
            alert("In Jquery");
            var res = $('input[type="radio"]:checked').val();

            if (res == '1') {
                $("#divFAFMQues").css("visibility", "hidden");
                $("#divFAFM").css("visibility", "hidden");
            }
            else {

                $("#divFAFMQues").css("visibility", "visible");
                $("#divFAFM").css("visibility", "visible");
            }
        });
    });
    </script>



</head>

The document.ready function is not getting fired when the page containing the user control is getting loaded. Please help.

当包含用户控件的页面被加载时,document.ready 函数不会被触发。请帮忙。

回答by Vijay

First, please check your jQuery file is importing correctly. Try checking with cdn first.

首先,请检查您的 jQuery 文件是否正确导入。首先尝试使用 cdn 进行检查。

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

Try to remove srcelement from your 2nd script tag:

尝试src从您的第二个脚本标签中删除元素:

<script language="javascript" type="text/javascript" src="~/Scripts/jquery-1.4.1.js">

to

<script language="javascript" type="text/javascript">

complete code:

完整代码:

<asp: Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
         $(document).ready(function() {
                    alert("In Jquery");
                });
    </script>
</asp:Content>