[英]Copy text from a text field and insert into another textarea at the current cursor position
參考下面的圖片,當我單擊添加按鈕時,我需要從第一個文本字段“規則前綴”復制文本,然后使用剪貼板,然后我需要將復制的文本插入到另一個textarea
“EditedEvent”中當前 cursor position。
我相信下面的代碼幾乎就在那里,但我似乎無法將復制的文本轉換為允許將其插入其他文本區域的格式。
完成此操作后,下一個任務是使用刪除按鈕執行相反的操作,並從textarea
“已編輯事件”中刪除復制的文本。
請注意, textarea
“已編輯事件”將被配置為只讀,因此用戶無法直接對其進行編輯。
// Function Rule Prefix Add Button
function rulePrefixAdd() {
/* Get the text field */
var copyText = document.getElementById("rulePrefixInput");
/* If text field is empty, show alert */
if (copyText.value.length == 0) {
/* Popup Window Alert the copied text */
alert("Text input is empty!");
return;
}
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
var textString = document.execCommand("copy");
//alert('textString'); // Alert box contains the correct string
// NOT WORKING - PLEASE HELP
typeInTextarea($(editedEventTextArea), textString);
// This works but I dont want a hardcoded string, need to insert the text from clipboard.
//typeInTextarea($(editedEventTextArea), " << LOOK WHAT I DID! >> ")
}
function typeInTextarea(el, newText) {
var start = el.prop("selectionStart")
var end = el.prop("selectionEnd")
var text = el.val()
var before = text.substring(0, start)
var after = text.substring(end, text.length)
el.val(before + newText + after)
el[0].selectionStart = el[0].selectionEnd = start + newText.length
el.focus()
return false
}
Just realized that the form-group attribute in the parent div class on the razor page was preventing the javascript function from inserting the text into the textarea. 參考上面的評論,我已經刪除了將文本復制到剪貼板的需要,更新了下面的 function 似乎工作正常。
// Function Rule Prefix Add Button
function rulePrefixAdd() {
/* Get the text field */
var copyText = document.getElementById("rulePrefixInput");
/* If text field is empty, show alert */
if (copyText.value.length == 0) {
/* Popup Window Alert the copied text */
alert("Text input is empty!");
return;
}
typeInTextarea($(editedEventTextArea), copyText.value);
}
typeinTextArea
方法的哪一部分不起作用? 是從剪貼板復制數據不起作用嗎? 如果您僅從rulePrefix
輸入復制數據,那么為什么甚至需要剪貼板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.