如何使用的document.ready的子页面功能 [英] how to use document.ready function on child pages

查看:104
本文介绍了如何使用的document.ready的子页面功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JavaScript在我的网站。当我创建新的网页,这是正常的JavaScript运行。当我创建一个新的网页,这是子页面,从母版页派生。此页面不支持我的JavaScript。我用这个code为多个单词自动完成属性。

我的code是在这里:

JavaScript的code内容占位符在头文件

 <%@页面语言=C#的MasterPageFile =〜/ Master_Front.masterAutoEventWireup =真
    codeFILE =Mailbox.aspx.cs继承=邮箱标题=邮件系统%>< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>    <链接HREF =样式/ UI的亮度/ jQuery的-UI-1.8.21.custom.css的rel =stylesheet属性类型=文/ CSS/>
    <脚本SRC =脚本/ jquery.min.js类型=文/ JavaScript的>< / SCRIPT>
    <脚本SRC =脚本/ jQuery的-ui.min.js类型=文/ JavaScript的>< / SCRIPT>
        <脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数(){
            SEARCHTEXT();
        });
        功能SEARCHTEXT(){
            $(#txtto)。自动完成({
                来源:函数(请求,响应){
                    $阿贾克斯({
                        键入:POST,
                        的contentType:应用/ JSON的;字符集= UTF-8,
                        网址:Mailbox.aspx / GetAutoCompleteData
                        数据:{用户名:+ extractLast(request.term)+'},
                        数据类型:JSON
                        成功:功能(数据){
                            响应(data.d);
                        },
                        错误:功能(结果){
                            警报(错误);
                        }
                    });
                },
                重点:函数(){
                    // prevent值插入焦点
                    返回false;
                },
                选择:函数(事件,UI){
                    VAR而言=拆分(THIS.VALUE);
                    //删除当前输入
                    terms.pop();
                    //添加所选项目
                    terms.push(ui.​​item.value);
                    //添加占位符来获得在最后逗号和空格
                    terms.push();
                    THIS.VALUE = terms.join(,);
                    返回false;
                }
            });
            $(#txtto)。绑定(的keydown,函数(事件){
                如果(event.key code $ === .ui.key code.TAB&放大器;&安培;
                        $(本)。数据(自动完成)。menu.active){
                    。事件preventDefault();
                }
            })
            函数split(VAL){
                返回val.split(/ \\ S * /);
            }
            功能extractLast(项){
                返回拆分(项).pop();
            }
        }
    < / SCRIPT>
< / ASP:内容>

C#code:

  [的WebMethod]
公共静态列表<串GT; GetAutoCompleteData(字符串USER_NAME)
{
    清单<串GT;结果=新的List<串GT;();
    SqlDataReader的博士= General.ReturnDR(选择的UserDetails DISTINCT mailid哪里mailid LIKE'%+ USER_NAME +%');
    而(dr.Read())
    {
        result.Add(DR [mailid]的ToString());
    }
    返回结果;
}


解决方案

您可以把的document.ready所有脚本这样当脚本存取权限它们的元素都准备好了。

  $(文件)。就绪(函数(){
    //把这里的孩子页面的所有脚本。
});

i need to use JavaScript on my website. When i create new web page,which is properly work with JavaScript. When i create a new web page, which is child page, derived from master page. this page does not support my JavaScript. I use this code for auto-complete property for multiple words.

My code is here:

JavaScript code in content place holder in header

<%@ 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# code:

[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;
}

解决方案

You can put all script in document.ready so that the elements are ready when script acces them.

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

这篇关于如何使用的document.ready的子页面功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆