当用户单击按钮时如何向文本区域添加文本

问题描述 投票:0回答:4

我想在用户单击按钮时将文本添加到文本区域。我知道如何连接字符串并将其添加到文本区域,但我想要的是将文本添加到用户单击的文本区域内的地点/位置,然后在该位置添加文本单击按钮时。

提前致谢

html onclick textarea
4个回答
4
投票

这是我的参考:

<script>
function input(){
    var text = "here the text that you want to input.";
    document.forms.form1.area.value = text;
}    
</script>
<form name='form1'>
    Click<input onclick='input()' type='button' value='BUTTON' id='button'><br>
    <textarea name='area'></textarea>

</form>

该示例显示当单击按钮时,一行文本添加到文本区域。 为什么我要在其中添加 javascript?因为仅 html 属性不足以实现它。
有帮助吗:D


4
投票

单击按钮时,您需要将特定位置(插入符号位置)的文本添加到文本区域。尝试以下操作:

var position;
	
    function getCaretPosition() {
        var ctlTextArea = document.getElementById('textArea');
        position = ctlTextArea.selectionStart;
        return position;
    }
	
	/* Needs JQuery */
    $(document).ready(function () {

        jQuery.fn.extend({
            insertAtCaret: function (myValue) {
                return this.each(function (i) {
                    if (document.selection) {
                        //For browsers like Internet Explorer
                        this.focus();
                        sel = document.selection.createRange();
                        sel.text = myValue;
                        this.focus();
                    }
                    else if (this.selectionStart || this.selectionStart == '0') {
                        //For browsers like Firefox and Webkit based
                        var startPos = this.selectionStart;
                        var endPos = this.selectionEnd;
                        var scrollTop = this.scrollTop;
                        this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
                        this.focus();
                        this.selectionStart = startPos + myValue.length;
                        this.selectionEnd = startPos + myValue.length;
                        this.scrollTop = scrollTop;
                    } else {
                        this.value += myValue;
                        this.focus();
                    }
                })
            }
        });

        $('#btnTest').click(function () {
            $("#textArea").insertAtCaret(' << inserted text! >> ');
        });

    });
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta charset="utf-8" />
    <title>How to add text to textarea when user clicks a button</title>
</head>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

<script>
// The javascript code goes here...
</script>

<body>
  
<h1>How to add text to textarea when user clicks a button</h1>
<div id="divDroppedFields">
    <textarea id="textArea" name="txtMessageFields" class="required" cols="80" rows="10" onclick="getCaretPosition()" onkeyup="getCaretPosition()">
	In the meantime the cat slowly recovered. The socket of the lost eye presented, it is true, a frightful appearance, but he no longer appeared to suffer any pain.
	</textarea>
</div>
<button id="btnTest">CLICK ME</button>


</body>
</html>


0
投票

使用 JS 的 HTML 示例,当您单击“Add someText”按钮时,将“someText”添加到文本区域的光标位置。

<div>
  <button onclick="addInplace('someText')">Add someText</button>
  <textarea></textarea>
</div>

点击你的元素激活js功能

function addInplace(text){
   //get the textarea either from function arg or in reference to event location
   let textArea=event.target.closest("div").querySelector("textarea") 
   if (!text || !textArea) return;
   navigator.clipboard.writeText(text) //optional add text to clipboard

   textArea.focus()
   let start=textArea.selectionStart
   let end=textArea.selectionEnd
   let val=textArea.value
   textArea.value=val.slice(0,start)+text+val.slice(end,val.length)
}

-1
投票

这是使用 JQuery 的简单方法。

你的html

<textarea id="txtarea"></textarea>
<input type="button" value="add text" id="add" />

你的 Jquery

$(document).ready(function(){
  $("#add").click(function(){
      $('#txtarea').html('test');
  });
});

您可以在此处查看其实际效果

© www.soinside.com 2019 - 2024. All rights reserved.