0

我有这个小问题。我的 html 页面中有这个 javascript:

function myfunction(form)
            {
                var name = document.details.txtName.value + "\n";
                var street = document.details.txtStreet.value + "\n";
                var city = document.details.txtCity.value + "\n";
                var stateProvince = document.details.txtStateProvince.value + "\n";
                var postalCode = document.details.txtPostalCode.value + "\n";
                var country = document.details.txtCountry.value + "\n";
                var homeTel = document.details.txtHomeTel.value + "\n";
                var workTel = document.details.txtWorkTel.value + "\n";
                var fax = document.details.txtFax.value + "\n";
                var mobile = document.details.txtMobile.value + "\n";
                var email = document.details.txtEmail.value + "\n";
                var enquiry = document.details.txtEnquiry.value + "\n";

                var message = (name +
                               street + 
                               city + 
                               stateProvince + 
                               postalCode + 
                               country + 
                               homeTel + 
                               workTel + 
                               fax + 
                               mobile + 
                               email +
                               enquiry);
                alert(message);
                location='mailto:somecrazyassemail@gmail.com?subject=Message From Redec Website&body=' + message;
                return false; //So that the page can stay at its current location.

在弹出的消息框中,它显示了我想要的字符串。

但是当这打开前景时,一切都是一长串。我怎样才能解决这个问题?}

4

2 回答 2

2

mailto 是特殊属性。您必须使用escape函数对字符串进行编码。

但是对于新行,您可以使用%0D%0A
有关更多信息,请参阅此站点

于 2013-04-06T13:36:42.160 回答
1

您将文本作为 HTML 插入,因此换行符'\n'被简单地解析为'n'. 要插入换行符,请使用相应的 HTML 元素<br>

例如:

var name = document.details.txtName.value + "<br>";
//                                           ^^^^
于 2013-04-06T13:21:50.103 回答