簡體   English   中英

在\\ n之后的特定行上將文本追加到textarea

[英]append text to textarea on specific line after \n

假設我想將文本附加到特定動態行上的文本textarea 因此,例如,我有一個checkboxes列表,其id范圍為0-i 現在,我還有一個textarea ,其行數與id范圍完全相同。 因此,如果用戶單擊復選框ID 2 ,我希望在我的textarea的第三行添加一些文本。

您是否需要先像這樣拆分textarea

var todolines = $('#todoListSave').val().split('\\n'); 然后分別附加文本還是有更好的方法?

你可以做類似的事情

 var $text = $('textarea'); //to initiate the value not required if the textarea value is already given $text.val(new Array($(':checkbox').length + 1).join('\\n')); $(':checkbox').change(function() { var split = $text.val().split('\\n'); if (this.checked) { split[this.id] += this.value; } else { var regex = new RegExp(RegExp.escape(this.value) + '$'); split[this.id] = split[this.id].replace(regex, '') } $text.val(split.join('\\n')); }) if (!RegExp.escape) { RegExp.escape = function(value) { return value.replace(/[\\-\\[\\]{}()*+?.,\\\\\\^$|#\\s]/g, "\\\\$&") }; } 
 textarea { min-height: 200px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="checkbox" id="0" value="1" /> <input type="checkbox" id="1" value="2" /> <input type="checkbox" id="2" value="3" /> <input type="checkbox" id="3" value="4" /> <input type="checkbox" id="4" value="5" /> <textarea></textarea> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM