我正在尝试解决我在ASP.Net应用程序中使用jQuery的对话框时遇到的问题。
我的网页有两个对话框tos_text
和tos_thankyou
,这些对话框当然是简单的<div>
标记,其中包含要显示的内容。
首先,我使用Google CDN来引用jQuery:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
我遇到的问题是,当我尝试附加jQuery对话框方法时,浏览器声称该对象上不存在对话框方法。由于我使用的是ASP.Net,我在运行时有条件地在Page_PreRender
事件处理程序中注入javascript:
protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "RegisterThankYouDialog",
"\n<script type='text/javascript'>$(function() { $('#tos_thankyou').dialog({ autoOpen: false, modal: true, buttons: { Ok : function() { $(this).dialog('close'); } } }); });</script>\n", false);
ScriptManager.RegisterStartupScript(this, GetType(), "RegisterTOSDialog",
"\n<script type='text/javascript'>$(function() { $('#tos_text').dialog({ autoOpen: false, modal: true, }); });</script>\n", false);
if (Show || ForceShow)
{
ScriptManager.RegisterStartupScript(this, GetType(), "ShowTOSDialog",
"\n<script type='text/javascript'>$(function() { $('#tos_text').dialog('open'); });</script>\n", false);
}
if (ShowThankYou)
{
ScriptManager.RegisterStartupScript(this, GetType(), "ShowThankYou",
"\n<script type='text/javascript'>$(function() { $('#tos_thankyou').dialog('open'); });</script>\n", false);
}
}
这是呈现的代码(来自Chrome JS控制台,包含错误):
<script type="text/javascript">
//<![CDATA[
(function() {var fn = function() {$get("ctl00_smScripts_HiddenField").value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();//]]>
</script>
<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'ctl00_NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>
<script type='text/javascript'>$(function() { $('#tos_thankyou').dialog({ autoOpen: false, modal: true, buttons: { Ok : function() { $(this).dialog('close'); } } }); });</script>
default.aspx:610Uncaught TypeError: Object [object Object] has no method 'dialog'
<script type='text/javascript'>$(function() { $('#tos_text').dialog({ autoOpen: false, modal: true, }); });</script>
<script type='text/javascript'>$(function() { $('#tos_text').dialog('open'); });</script>
<script type="text/javascript">
//<![CDATA[
Sys.Application.add_init(function() {
$create(Sys.Extended.UI.ModalPopupBehavior, {"BackgroundCssClass":"modalBackground","CancelControlID":"ctl00_MainContent_btnPrivacyOK","PopupControlID":"ctl00_MainContent_pnlPrivacy","dynamicServicePath":"/default.aspx","id":"ctl00_MainContent_ModalPopupExtender1"}, null, null, $get("ctl00_MainContent_btnPrivacy"));
});
//]]>
</script>
答案 0 :(得分:5)
dialog方法是jquery-ui脚本的一部分。将其添加到您的页面:
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
您可能还想添加相关的样式表:
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
答案 1 :(得分:0)
您需要加载jQuery UI才能获得“对话框”。这是谷歌的CDN
jQuery UI名称:jqueryui最新版本:1.8.16(查看较旧版本) 加载请求:google.load(“jqueryui”,“1.8.16”);演员: uncompressed:true(如google.load(“jqueryui”,“1.8.16”, {未压缩:真});路径: https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js 路径(U): https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js site:http://jquery.com/注意:这个库依赖于jquery。您 还必须在加载此模块之前加载jquery。版本1.8.3不是 由于其寿命短而托管,别名1.8.3实际加载 1.8.4。