[英]jQuery document.ready throws error
我正在將jQuery multiselect控件動態添加到這樣的頁面上:
var captionCell = new HtmlTableCell { InnerHtml = control.Caption };
var inputCell = new HtmlTableCell();
inputCell.Controls.Add(inputControl);
var row = new HtmlTableRow();
row.Cells.Add(captionCell);
row.Cells.Add(inputCell);
tbl.Rows.Add(row);
然后像這樣構建我的javascript字符串:
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script type=\"text/javascript\">");
sb.AppendLine("var $callback = $(\"#callback\");");
sb.AppendLine("$(document).ready(function () {");
sb.Append("$(\"#");
sb.Append(multiSelectControl.ClientID);
sb.AppendLine("\").multiselect(");
sb.AppendLine("{");
sb.AppendLine("show: \"fade\",");
sb.AppendLine("hide: \"fade\",");
sb.AppendLine("click: function (event, ui){");
sb.AppendLine("$callback.text(ui.text + ' ' + (ui.checked ? 'checked' : 'unchecked'));");
sb.AppendLine("},");
sb.AppendLine("});");
sb.AppendLine("});");
sb.AppendLine("</script>");
然后將腳本添加到頁面中,如下所示:
ScriptManager.RegisterClientScriptBlock(this.Page, Page.GetType(), "CreateControl" + inputControl.ClientID,
sb.ToString(), false);
但是即時通訊嘗試執行此操作時收到以下錯誤:
Microsoft JScript運行時錯誤:對象不支持此屬性或方法
請協助大家。
提前致謝。
您需要在頁面中包含jQuery才能使用document.ready,您沒有在頁面中添加腳本標簽以包含jquery,沒有添加腳本標簽以包含jquery。
<script type="text/javascript" src="/jQueryFolder/jquery.js"></script>
var $callback = $("#callback");
$(document).ready(function() {
$("#ClientID").multiselect({
show: "fade",
hide: "fade",
click: function(event, ui) {
$callback.text(ui.text + ' ' + (ui.checked ? 'checked' : 'unchecked'));
}
});
});
我通過創建用戶控件來解決此問題,當我需要使用上面的腳本時,我剛剛加載了用戶控件。
解決了所有問題,並且效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.