开发者

Line breaks dissapearing in HTML submission

I am having an issues where line breaks are dissapearing along a form submission path. Here is my scenario.

I have a multi-line textbox (2 rows) on a html web page. When the form is submitted, jquery retrieves the value of the textbox using

$("#txtboxid").val();

When I inspect the value using Chrome's debugger I can see that line breaks are in there.

(test data was a\r\nb\r\nc\r\nd)

I then get jQuery to post the data using the following JavaScript.

function postData(to, params) {
    var myForm = document.createElement("form");
    myForm.method = "post";
    myForm.action = to;
    for (var k in params) {
        var myInput = document.createElement("input");
        myInput.setAttribute("name", k);
        myInput.setAttribute("value", params[k]);
        myForm.appendChild(myInput);
    }
    document.body.appendChild(myForm);
    myForm.submit();
    document.body.removeChild(myForm);
}

When I then inspect the posted values in ASP.NET using the NameValueCollection from Request.Form, there do not seem to be any line breaks or line break charac开发者_如何学JAVAters.

eg = string textboxValue = Request.Form["myTextBox"];

Any ideas why?


This is expected behavior: you're taking data from multiline <textarea> elements and putting it in single-line <input> elements. Therefore, line breaks are lost.

Try creating <textarea> elements instead of <input> elements. For instance, using jQuery:

function postData(to, params)
{
    var myForm = $("<form>").attr({
        method: "post",
        action: to
    });
    $.each(params, function(key, value) {
        $("<textarea>").attr("name", key).val(value).appendTo(myForm);
    });
    myForm.appendTo("body").submit().remove();
}


Your data in coming from multi-line textboxes but you are creating INPUT elements which I guess dont accept newlines. That could be an issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜