【问题标题】:Copy text from a text field and insert into another textarea at the current cursor position从文本字段复制文本并插入到当前光标位置的另一个文本区域
【发布时间】:2020-01-22 21:10:15
【问题描述】:

参考下面的图片,当我点击添加按钮时,我需要从第一个文本字段“规则前缀”复制文本,然后使用剪贴板,然后我需要将复制的文本插入到另一个 textarea当前光标位置的“EditedEvent”。

我相信下面的代码几乎就在那里,但我似乎无法将复制的文本转换为允许将其插入其他文本区域的格式。

一旦实现这一点,接下来的任务就是使用删除按钮执行相反的操作,并从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
}

【问题讨论】:

  • (offtopic) 规则前缀却出现在“SampleStringblahblahblah”中间...你在构建什么?您是如何在不可编辑区域的中间进行选择的?
  • 你在同一个问题中设法说:“粘贴在当前光标位置”“textarea不可编辑”.. .
  • 因此,如果 textarea 确实是可编辑的(不是只读的)(因此可以将插入符号放入其中),那么如果插入符号位于前缀规则的中间并且您想要添加另一个?比嵌套的 rules(?) 应该如何使用“删除”?
  • 好吧,也许只读是基于 Web 的应用程序的错误术语,我让这一切都在 Windows 窗体中工作,但尝试使用 javascript 将相同的原因和影响应用于 asp.net 核心显然是一个不同的水壶鱼。不要过多地阅读我称之为文本字段规则前缀的事实,我也需要其他表单字段的相同逻辑,所以在工作解决方案之后。谢谢

标签: javascript razor-pages


【解决方案1】:

刚刚意识到剃刀页面上的父 div 类中的 form-group 属性阻止了 javascript 函数将文本插入到 textarea 中。参考上面的 cmets,我已经删除了将文本复制到剪贴板的需要,下面的更新功能似乎可以正常工作。

 // 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);
}

【讨论】:

    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    相关资源
    最近更新 更多