如何使用文档.子页面上的Ready函数

本文关键字:Ready 函数 何使用 文档 | 更新日期: 2023-09-27 18:07:04

我需要在我的网站上使用JavaScript。当我创建新的网页,这是正确的工作与JavaScript。当我创建一个新的网页,这是子页面,从母版页派生。这个页面不支持我的JavaScript。我将此代码用于多个单词的自动完成属性。

我的代码在这里:

标题

内容占位符中的JavaScript代码
<%@ Page Language="C#" MasterPageFile="~/Master_Front.master" AutoEventWireup="true"
    CodeFile="Mailbox.aspx.cs" Inherits="Mailbox" Title="Mail System" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="Style/ui-lightness/jquery-ui-1.8.21.custom.css"rel="stylesheet" type="text/css" />
    <script src="script/jquery.min.js" type="text/javascript"></script>
    <script src="script/jquery-ui.min.js" type="text/javascript"></script> 
        <script type="text/javascript">
        $(document).ready(function() {
            SearchText();
        });
        function SearchText() {
            $("#txtto").autocomplete({
                source: function(request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Mailbox.aspx/GetAutoCompleteData",
                        data: "{'username':'" + extractLast(request.term) + "'}",
                        dataType: "json",
                        success: function(data) {
                            response(data.d);
                        },
                        error: function(result) {
                            alert("Error");
                        }
                    });
                },
                focus: function() {
                    // prevent value inserted on focus
                    return false;
                },
                select: function(event, ui) {
                    var terms = split(this.value);
                    // remove the current input
                    terms.pop();
                    // add the selected item
                    terms.push(ui.item.value);
                    // add placeholder to get the comma-and-space at the end
                    terms.push("");
                    this.value = terms.join(", ");
                    return false;
                }
            });
            $("#txtto").bind("keydown", function(event) {
                if (event.keyCode === $.ui.keyCode.TAB &&
                        $(this).data("autocomplete").menu.active) {
                    event.preventDefault();
                }
            })
            function split(val) {
                return val.split(/,'s*/);
            }
            function extractLast(term) {
                return split(term).pop();
            }
        }
    </script>
</asp:Content>
c#代码:

[WebMethod]
public static List<string> GetAutoCompleteData(string user_name)
{
    List<string> result = new List<string>();
    SqlDataReader dr=General.ReturnDR("select DISTINCT mailid from UserDetails where mailid LIKE '%"+user_name+"%'");
    while (dr.Read())
    {
        result.Add(dr["mailid"].ToString());
    }
    return result;
}

如何使用文档.子页面上的Ready函数

您可以将所有脚本放在文档中。准备好了,这样当脚本访问元素时,元素就准备好了。

$(document).ready(function(){
    //put all your script of child page here.
});