jQuery document.ready抛出错误

时间:2012-12-06 06:19:27

标签: c# jquery asp.net

我正在动态地将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运行时错误:对象不支持此属性或方法

请协助他们。

提前致谢。

enter image description here

3 个答案:

答案 0 :(得分:1)

您需要在页面中包含jQuery才能使用document.ready,您没有添加脚本标记以在页面中包含jquery,添加脚本标记以包含jquery。

<script type="text/javascript" src="/jQueryFolder/jquery.js"></script>

答案 1 :(得分:0)

  var $callback = $("#callback");
 $(document).ready(function() {
    $("#ClientID").multiselect({
    show: "fade",
    hide: "fade",
    click: function(event, ui) {
        $callback.text(ui.text + ' ' + (ui.checked ? 'checked' : 'unchecked'));
    }
});
});​

答案 2 :(得分:0)

我通过创建用户控件解决了这个问题,当我需要使用上面的脚本时,我只是加载了用户控件。

解决了所有事情并且工作得很好。