简体   繁体   English

用换行符代替(逗号) <br> 在jQuery中

[英]Replacing ,(comma) with line break <br> in jquery

I am using below code to get selected values from multiple select and displaying these values in a textarea. 我正在使用下面的代码从多个选择中获取选定的值,并在文本区域中显示这些值。

$(document).ready(function(){
  $("#client").change(function(){
   var sel = $("#client").val();
          $("#clientselected").text(sel);
     });
});

Selected values are separated by comma, now I want each selected value in each line in textarea. 选定的值用逗号分隔,现在我要在textarea的每一行中选择每个选定的值。 I tried replacing , with <br> using replace function, but I was unable to. 我试图取代,<br>使用replace功能,但我无法。 How can I do this? 我怎样才能做到这一点?

Line breaks in a <textarea> element are made with \\n not html breaks. <textarea>元素中的换行符是\\n而不是html换行符。 You should also set the value. 您还应该设置该值。

$("textarea").val("a line\nand another\nandanother");

Pretty sure you want to replace the <br/> with \\n , if it's in a text area (which won't know what to do with HTML tags, in general. 如果位于文本区域(通常不知道如何处理HTML标记),可以肯定要用\\n替换<br/>

http://jsfiddle.net/KYMUc/1/ http://jsfiddle.net/KYMUc/1/

  • Since you have a multiple select, .val() will return an array of option values so you can use .join() to with \\n as the seperator 由于您有多个选择, .val()将返回选项值的数组,因此您可以使用.join()\\n作为分隔符
  • You need use .val() to set the value to the clientselected element 您需要使用.val()将值设置为clientselected元素

So 所以

$(document).ready(function () {
    $("#client").change(function () {
        var sel = $("#client").val();
        $("#clientselected").val(sel.join('\n'));
    });
});

Demo: Fiddle 演示: 小提琴

Use newline ("\\n") or " &#13; " instead of 使用换行符(“ \\ n”)或“ &#13; ”代替
to make a line break 换行

$(document).ready(function () {
    $("#client").change(function () {
        var sel = $("#client").val();
        $("#clientselected").html(sel.join("\n") + "\n");
    });
});

Here is the fiddle 这是小提琴

try this: 尝试这个:

$("#clientselected").html(sel.split(",").join("\n"));

demo 演示

Jquery 或 javascript 添加一个换行符<br>在 a 中的 x 个字符之后<div></div><div id="text_translate"><p>我正在寻找一种在&lt;div&gt;中仅前 4 或 5 个字符之后插入&lt;br /&gt;的方法。</p><p> 示例: &lt;div id="wine-name"&gt; 2008 年赤霞珠&lt;/div&gt;</p><p> 显示如下:<br><br> <strong>2008年</strong><strong><br></strong><strong>赤霞珠</strong></p><p>不确定 javascript 或 jQuery 哪个更容易。 该网站已经在使用 jQuery。</p><p> 有任何想法吗?</p></div> - Jquery or javascript to add one line break <br /> after x amount of characters in a <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery / Javascript-转 <br> 内部div到换行符? - Jquery / Javascript - Turn <br> inside div to line break? jQuery:如何添加一个 <br/> 用不同的字符串换行来分隔2中的句子? - jQuery: howto add a <br/> line break in different strings to separate sentences in 2? 将新行替换为<br>标签快递 javascript - Replacing new line with <br> tag express javascript BR使用正则表达式替换新行的问题 - BR replacing issues using regex for new line jQuery正则表达式-无法替换换行符 <br> 在img标签的title属性中 - jQuery regex - cannot replace break/new line to <br> in img tag' s title attribute Jquery 或 javascript 添加一个换行符<br>在 a 中的 x 个字符之后<div></div><div id="text_translate"><p>我正在寻找一种在&lt;div&gt;中仅前 4 或 5 个字符之后插入&lt;br /&gt;的方法。</p><p> 示例: &lt;div id="wine-name"&gt; 2008 年赤霞珠&lt;/div&gt;</p><p> 显示如下:<br><br> <strong>2008年</strong><strong><br></strong><strong>赤霞珠</strong></p><p>不确定 javascript 或 jQuery 哪个更容易。 该网站已经在使用 jQuery。</p><p> 有任何想法吗?</p></div> - Jquery or javascript to add one line break <br /> after x amount of characters in a <div> HTML换行符不带 <br/> 或\\ n? - HTML line break without <br/> or \n? 如何不使用而强制换行<br/> - How to force a line break without using <br/> 如何使用br中断线制作(复制到剪贴板) - How to make (Copy to clipboard )with br break line 如何显示换行符<br>在 javascript 中? - How to display a line break <br> in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM