【问题标题】:jQuery Popup not displaying string with square brackets insidejQuery Popup 不显示带有方括号的字符串
【发布时间】:2016-01-27 11:37:36
【问题描述】:

我已经坚持了一段时间。我不太确定问题出在哪里,因为它不会引发任何错误。此代码适用于大多数字符串。但是,我注意到只要字符串中有特殊字符我希望它显示,jQuery Popup 函数就不会运行。在这种情况下,由于我的字符串中有“[”和“]”,它似乎没有弹出。我尝试用“(”和“)”替换“[”和“]”,但它似乎仍然没有按预期工作。

这是我的示例代码:

string test = tbICD91.Text; //<--"J43.0"
string codeDescription = string.Empty;
codeDescription = dt.Rows[0][0].ToString().Trim(); //<--"Unilateral pulmonary emphysema [MacLeod's syndrome]"
if (String.IsNullOrEmpty(codeDescription))
{
    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Cannot find description to code in textbox');", true);
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "ScrollPage", "ResetScrollPosition();", true);
}
else
{
    ClientScript.RegisterStartupScript(this.GetType(), "Popup", string.Format("ShowPopup('{0}: {1} ');", test, codeDescription), true);
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "ScrollPage", "ResetScrollPosition();", true);
}
return;

这是我在前端使用 div 的 jQuery 弹出函数。

<script type="text/javascript">
    function ShowPopup(message) {
        $(function () {
            $("#dialog").html(message);
            $("#dialog").dialog({
                title: "ICD Code Description",
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
        });
    };
</script>

<div id="dialog" style="display: none"></div>

我一直在尝试各种转义字符串或文字字符串,例如@"Unilateral pulmonary emphysema [MacLeod's syndrome]",并用其他东西替换方括号,但它似乎不起作用。我不确定它是否因为 jQuery 或 C# 而无法正常工作,因为它没有抛出任何错误。只要字符串中出现方括号,它似乎就会跳过弹出窗口。有什么帮助吗?

【问题讨论】:

    标签: javascript c# jquery asp.net


    【解决方案1】:

    问题不在于方括号,问题在于MacLeod's Syndrome 中的单引号' 提前终止了ShowPopup 的参数。转义该引用以解决问题。

    codeDescription = dt.Rows[0][0].ToString().Trim().Replace("'", @"\'");
    

    【讨论】:

    • 哇!我不敢相信我没有看到那个。感谢您帮助我解决这个简单的问题……嗯,这很尴尬……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多