[英]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/>
。
.val()
will return an array of option values so you can use .join()
to with \\n
as the seperator .val()
将返回选项值的数组,因此您可以使用.join()
以\\n
作为分隔符 .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'));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.