[英]web form doesnt call jquery library when its using a master page, but works without a master page
我正在嘗試使用名為“ chosen”的jQuery庫進行下拉列表,並列出更好的列表。 這是選擇的jquery庫: http : //harvesthq.github.io/chosen/
如果我僅使用以下內容創建一個Web表單:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AssetTracker.Chosen.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script language="Javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="../chosen.jquery.js" type="text/javascript"></script>
<script src="docsupport/prism.js" type="text/javascript" charset="utf-8"></script>
<link href="/Content/Site.css" rel="stylesheet" />
<link rel="stylesheet" href="/Chosen/chosen.css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/cupertino/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function() {
$("#DropDownList1").chosen();
});
</script>
<title>test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList CssClass="chosen-single" ID="DropDownList1" runat="server" data-placeholder="Choose one...">
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>Russia</asp:ListItem>
<asp:ListItem>Poland</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
它工作正常,我可以打電話給:
.chosen()
沒有錯誤,還有漂亮的下拉列表。 現在的問題是我有一個使用如下母版頁的asp.net Web表單:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="AssetTracker.Chosen.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script language="Javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="../chosen.jquery.js" type="text/javascript"></script>
<script src="docsupport/prism.js" type="text/javascript" charset="utf-8"></script>
<link href="/Content/Site.css" rel="stylesheet" />
<link rel="stylesheet" href="/Chosen/chosen.css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/cupertino/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function () {
$("#MainContent_DropDownList1").chosen();
});
</script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="MainContent" runat="server">
<div>
<asp:DropDownList CssClass="chosen-single" ID="DropDownList1" runat="server" data-placeholder="Choose one...">
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>Russia</asp:ListItem>
<asp:ListItem>Poland</asp:ListItem>
</asp:DropDownList>
</div>
</asp:Content>
相同的解決方案,相同的項目,但在控制台中具有母版頁的項目卻給我一個錯誤:
Uncaught TypeError: Object [object Object] has no method 'chosen'
我知道一般來說,此錯誤是由於無法找到腳本文件而導致的...但是正如我所說的,這是相同的位置,相同的解決方案,相同的項目...但是對於我來說,我一生都無法找到它工作。
如果有人需要,這里是母版頁:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="AssetTracker.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title>Asset Tracker</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
<form runat="server">
<asp:ScriptManager runat="server">
<Scripts>
<%--Framework Scripts--%>
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
<header>
<div class="content-wrapper">
<div class="float-left">
</div>
<div class="float-right">
<section id="login">
<asp:LoginView runat="server" ViewStateMode="Disabled">
<LoggedInTemplate>
<p>
Hello, <a runat="server" class="username" href="#" title="">
<asp:LoginName runat="server" CssClass="username" />
</a>
</p>
</LoggedInTemplate>
</asp:LoginView>
</section>
<nav>
<ul id="menu">
<li><a runat="server" href="~/">Home</a></li>
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
<section class="content-wrapper main-content clear-fix">
<asp:ContentPlaceHolder ID="MainMessageContent" runat="server"/>
<asp:ContentPlaceHolder ID="ButtonContent" runat="server"/>
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© <%: DateTime.Now.Year %></p>
</div>
</div>
</footer>
</form>
</body>
</html>
這與jquery庫之間的某些沖突有關,但是我對Site.master
網站母版頁的了解不足以理解為什么會這樣。 請有人讓我知道我哪里出問題了嗎?
問題是您有到“選擇的” javascript文件的相對路徑嗎?
如果顯示的頁面的URL(在任何特定頁面上)沒有相同的路徑,則“選擇”文件的路徑的../
部分將不正確。
也許如果您為腳本使用完全指定的URL。
首先確保您已加載JavaScript,然后...問題是因為您使用的是母版頁,它將更改頁面上元素的客戶端ID
所以你可以用這個為例
<asp:Content ID="Content4" ContentPlaceHolderID="MainContent" ClientIDMode="Static" runat="server">
使用母版頁時,我經歷了很多次。 首先,我認為您應該從母版頁中刪除對Jquery庫的任何引用。 請記住,當您多次加載Jquery庫時,它將停止工作。 因此,如果您使用此<asp:ScriptReference Name="jquery" />
,則在內部再次調用Jquery庫,因此請將其從MasterPage中刪除。 目的是只調用一次Jquery庫,以避免此常見錯誤Uncaught TypeError: Object [object Object] has no method 'xxxxx'
。 希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.