DBDataSet.tblAdsDataTable dtAds = client.getAdsDate(ref errCode, AdsType, AdsSlot, Country);
        string ss = "";
        string aa = "";
        if (dtAds.Rows.Count > 0)
        {
            foreach (DBDataSet.tblAdsRow dr in dtAds)
            {
                DateTime date = dr.AdsDate;
                string AdsDate = date.ToString("dd/MM/yyyy");

                ss += '"' + AdsDate + '"' +",";
                aa = ss.TrimEnd(','); // equal to "01/01/2018","02/01/2018"

                string jquery = "var naArray = ["+ aa +"];";;
                ClientScript.RegisterStartupScript(typeof(Page), "a key","<script>" + jquery + "</script>");
            }
        }


嗨,从上面的代码,我想在<script> var naArray = ["01/01/2018","02/01/2018"];</script>后面的代码中添加javascript。但是,当我执行时,它成为数组中缺少“ 02/01/2018”的<script> var naArray = ["01/01/2018"]; <script>。感谢是否有人可以为此提供帮助。谢谢。

最佳答案

您正在将值设置为foreach循环内的jquery变量。它应该在循环之外。尝试如下更新代码。

DBDataSet.tblAdsDataTable dtAds = client.getAdsDate(ref errCode, AdsType, AdsSlot, Country);
string ss = "";
string aa = "";
if (dtAds.Rows.Count > 0)
{
    foreach (DBDataSet.tblAdsRow dr in dtAds)
    {
        DateTime date = dr.AdsDate;
        string AdsDate = date.ToString("dd/MM/yyyy");

        ss += '"' + AdsDate + '"' +",";
    }
    aa = ss.TrimEnd(','); // equal to "01/01/2018","02/01/2018"
    string jquery = "var naArray = ["+ aa +"];";;
    ClientScript.RegisterStartupScript(typeof(Page), "a key","<script>" + jquery + "</script>");
}

关于javascript - C#-从后面的代码添加javascript函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50809127/

10-17 00:24