jQuery:如何添加换行符以形成输入? [英] jQuery: How can I add line break to form input?

查看:291
本文介绍了jQuery:如何添加换行符以形成输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在收集标准HTML表单中的信息。例如,我有< input type =textname =UserNameid =name/> 。提交表单时,我想为输入的值添加换行符。因此,如果用户输入foo,表单会提交值foo \ n。



下面是我使用的jQuery函数:



$ $ $ $ $ $ $ $ $#formID。submit(function(){
$(:text)。each(function( ){
var value = $(this).val();
var newValue = value +\\\
;
$(this).val(newValue);
});
});

然而,提交表单时,赋给表单字段的值没有换行符。

我的目标是让换行符在生成电子邮件的后端表单处理输出中生存下来。由于我无法控制生成邮件的脚本,因此我试图将某些格式设置为可以控制的格式。



任何帮助都是值得赞赏的。 / p>

解决方案

在看到您的评论之前,我发布了以前的答案,输出是纯文本。现在试试这个:

  $(document).ready(function(){
$(#formID) .submit(function(){
$(:text)。each(function(){
var value = $(this).val();
var myname = $(这个).attr('name');
var newValue = value +\\\
;
var hid ='< input type =hiddenname ='+ myname +' value =''+ newValue +'/>';
$(this).removeAttr('name');
$(#formID)。append(hid);
});
});
});






上一个回答



正如Mark所说,在浏览器(或电子邮件客户端)中查看换行符时,不会将换行符显示为换行符,因此不必添加换行符\ n,你应该添加< br />

  $( (function)(){
$(:text)。each(function(){
var value = $(this).val();
var newValue = value +'< br />';
$(this).val(newValue);
});
})


I'm collecting information in a standard HTML form. I have, for example, <input type="text" name="UserName" id="name"/>. When the form is submitted, I want to add a line break to the value entered. So if the user entered "foo," the form would submit the value "foo\n."

Here's the jQuery function I'm using:

$("#formID").submit(function () {
    $(":text").each(function () {
        var value = $(this).val();
        var newValue = value + " \n";
        $(this).val(newValue);
    });
});

When the form is submitted, however, the value assigned to the form field does not have the line break.

My goal is for the line break to survive the output of the back-end form processing, which generates an email. Since I don't have control over the script that generates the mail, I'm trying to impose some formatting with what I can control.

Any assistance is appreciated.

解决方案

I posted my previous answer before I saw your comment that the output is plain text. Now try this:

$(document).ready(function() {  
    $("#formID").submit(function () {
        $(":text").each(function () {
            var value = $(this).val();
            var myname  = $(this).attr('name');
            var newValue = value + " \n";
            var hid   = '<input type="hidden" name="' + myname + '" value="' + newValue + '"/>';
            $(this).removeAttr('name');
            $("#formID").append(hid);
        });
    });
});


Previous Answer

As Mark says, the line break character is not rendered as a line break when viewed in the browser (or email client for that matter), so instead of adding a line break character "\n", you should add a <br/>

$("#formID").submit(function () {
    $(":text").each(function () {
        var value = $(this).val();
        var newValue = value + '<br/>';
        $(this).val(newValue);
    });
})

这篇关于jQuery:如何添加换行符以形成输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆