0

因此,我使用 C# 动态生成 jQuery 并将其发送到网页。 问题是它似乎根据文件和 Js Fiddle 生成了正确的 jQuery,但它实际上在页面上不起作用。

jsFiddle 在这里http://jsfiddle.net/ER2hE/

现在我查找了如何将 javacript 发送到网站。它应该像这样工作。 http://msdn.microsoft.com/en-us/library/bb359558.aspx

我的代码就是这种方法

    private void sendScript(string script)
    {
        const string someScript = "alertMe";
        //send the built script to the website.
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), someScript, script, true);
    }

这非常简单,它适用于其他代码调用。但在这种情况下它没有。

调用它的代码在我的 C# 中是这样的

    private void populateGroups()
    {
        //this generates correct javascript according to the file and JS fiddle but unfortunately doees not work.

        string splitme = "USE ACES SELECT GroupName, GroupID FROM PrimaryGroup ORDER BY GroupName";
        DataTable dt = fillDataTable(splitme);
        string script = "";

        foreach (DataRow dr in dt.Rows)
        {
            //add the locations to the <select> box
            script += " $('#groupList').append('<option value=\" " + dr.ItemArray[1].ToString() + " \"> " + dr.ItemArray[0].ToString() + " </option>');  ";
        }

        sendScript(script);
        JSErrorLog(script, "GROUPS");
}

整个事情在启动时被调用

    protected void Page_Load(object sender, EventArgs e)                
    {
        if (this.IsPostBack == false)
        {
            populateMakes();
            populateLocation();
            populateGroups();
        }
    }

它生成的 jQuery 也适用于 JSFiddle 我从一个方法中提取它,该方法编写它在此处调用的方法中生成的 javascript 是小提琴 JSErrorLog。

http://jsfiddle.net/ER2hE/

哦,我的 aspx 文件中的 html 看起来像这样

    <div class="row2">
       <span>Group</span>
        <select id="groupList" multiple="multiple" onclick="setGroups()" class="normalsize">
        </select>
    </div>

我相信这就是一切。我只是想让我的东西工作。我愿意发布任何其他代码,请问。如果您知道它为什么不起作用,请告诉我。

4

2 回答 2

1

它何时实际执行该代码?DOM中存在id为“groupList”的元素之前还是之后?我的猜测是之前。

解决方案?将您的代码包装在 document.ready 处理程序中。

jQuery(function($) {
     $('#groupList').append('<option value=" 46 "> AC Units </option>');
     // etc etc
});
于 2013-08-30T16:13:12.513 回答
0

返回简单的字符串 js 代码。并使用 eval() 运行它

于 2013-08-30T23:48:33.507 回答