简体   繁体   中英

TinyMCE how to dynamically get line breaks in a textarea

I need advice on how I can create dynamic line breaks ideally with an <li> inside TINY. I have tried using an each loop and also a for loop, and the values just do not appear in the text area. BUT, if I just add them to the text area with a val() they go in fine, BUT as just one long string.

The text area has an id of wo_materials. I'm successfully getting my text into Tiny like this:

        $('#wo_materials').val(materials);

        tinymce.init({
            selector:'textarea'
        });

And I get a nice row of text values:

TinyMCE中的值

The materials value is an array. If I look at it in the console it looks like this:

0: BP #15 Plain Felt 36" 
1:  Duraflo Weatherpro 50 
2:  1 1/4 Coil Nails - box

Thanks !

如果您只在初始化tinymce之前修改文本区域的值,那么这可能对您有用:

$('#wo_materials).val(materials.join('<br/>'));

This works. The secret is adding the incrementing var "text" to the for loop and then wrapping the object / array in an <li>

var materials= JSON.parse(localStorage.getItem('materials'));
var text=" "; 
materials.length;
function workorders(){
            for (i = 0; i < materials.length; i++) {
               text += "<li>"+materials[i]+"</li>";
            }
            $('#wo_materials').val("<li>"+text+"</li>");
        }

        workorders();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM