尝试在代码后面添加 jQuery 时,输入字符串的格式不正确

Sar*_*nyu 0 c# asp.net

我尝试这样做,它给出了我调用的行标题中提到的错误String.Format。

public static void JqueryDialogue(string divId)
{
    String script = String.Format(
        "$(document).ready(function(){ $('#{0}').dialog('open'); });", 
        divId);

    // Gets the executing web page
    Page page = HttpContext.Current.CurrentHandler as Page;
    string codeId = "openDialoge" + divId.ToString();

    // Checks if the handler is a Page and that the script isn't already on Page
    if (page != null && !page.ClientScript.IsClientScriptBlockRegistered(codeId))
    {
        page.ClientScript.RegisterStartupScript(
            typeof(JavascriptHelper), 
            codeId, 
            script,
            true);
    }
} 
Run Code Online (Sandbox Code Playgroud)

Joã*_*elo 5

如果您使用,String.Format则需要转义您想要按字面输出的{和字符,因为它们是 Javascript 代码。}为了实现这一点,您分别使用{{和}}。

您可以在此处阅读有关字符串格式的更多信息,其中还解释了因转义大括号而导致的奇怪行为。