使用Javascript在textarea中添加换行符

时间:2013-12-02 14:26:22

标签: c# javascript

我有一个iframe弹出窗口,允许用户添加信息。添加之后,我希望将它添加到一些隐藏的输入中(在这种情况下是textareas)。

我需要使用以下C#代码添加连接的地址字符串:

public string AddressInputFormatted { get { return string.Format("{0}{4}{1}, {2} {3}", Address, City, State, Zip, Environment.NewLine); } }

或甚至(尝试过两者):

public string AddressInputFormatted { get { return string.Format("{0}\n{1}, {2} {3}", Address, City, State, Zip); } }

我得到了并在回发时创建了一个启动脚本:

    Dim strScript As String = "window.parent.$('.contact.name').html('{0}');" & _
                    "window.parent.$('.contact.title').html('{1}');" & _
                    "window.parent.$('.contact.email').html('<a href=mailto:{2}>{2}</a>');" & _
                    "window.parent.$('.contact.address').html('{3}');" & _
                    "window.parent.$('textarea.contact.address').val('{9}');" & _
                    "window.parent.$('.contact.work-phone').html('Phone: {4}');" & _
                    "window.parent.$('.contact.mobile-phone').html('Mobile: {5}');" & _
                    "window.parent.$('.contact.fax').html('Fax: {6}');" & _
                    "window.parent.$('#hiddenCustomerID').val({7});" & _
                    "window.parent.$('.getCustomerName').val('{8}'); window.parent.$('.customer.name').html('{8}'); parent.$.fn.colorbox.close();" & _
                    "window.parent.$('.change-contact').show(); window.parent.$('.add-contact, .autoCompleteCustomerContacts').hide();" & _
                    "window.parent.$('.change-customer').show(); window.parent.$('.add-customer, .autoCompleteCustomersWithID').hide();"

strScript = String.Format(strScript, contact.FullName, contact.Title, contact.Email, contact.AddressInputFormatted, contact.WorkPhone, contact.MobilePhone, contact.Fax, contact.CustomerID, customer.Name, contact.Address)
ScriptManager.RegisterStartupScript(Page, Me.GetType(), "updateParent", " $(document).ready(function () {" & strScript & " });", True)

无论哪种方式,我都会在页面上看到实际的javascript并返回一行,并且出现javascript错误:

SyntaxError: Unexpected token ILLEGAL

我做错了什么?如何使用javascript在隐藏文本区域中存储新行?

1 个答案:

答案 0 :(得分:1)

您需要转义转义字符,以便HTML输出包含\n

public string AddressInputFormatted
{
    get
    {
        return string.Format("{0}\\n{1}, {2} {3}", Address, City, State, Zip);
    }
}