Javascript 使用 Jquery 将文本插入输入/文本区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4961213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:58:16  来源:igfitidea点击:

Insert text into the input/textarea using Jquery

javascriptjquery

提问by Ambo100

<div class="ctrlHolder">
<label for="" id="name.label">Name</label>
<input name="name" id="name" type="text" class="textInput small" />
<p class="formHint">The name of the item you are submitting</p>
</div>

How can I insert predefined text into a input element. I'd like this function to active when the user doubleclicks the label element.

如何将预定义文本插入到输入元素中。当用户双击标签元素时,我希望此功能处于活动状态。

$('#name.label').dblclick(function(){
    $('#name').val('some text');
});

回答by Rafay

herecheck this fiddle

在这里检查这个小提琴

you can use .text("yourPreDefinedText")to replace the text of an element

您可以使用.text("yourPreDefinedText")来替换元素的文本

回答by Colin O'Dell

$('#name.label') will cause jQuery to look for an element with id "name" and class "label". I believe the code above should work if you rename the label's id to something like "name-label".

$('#name.label') 将导致 jQuery 查找具有 id "name" 和 class "label" 的元素。我相信如果您将标签的 id 重命名为“name-label”之类的东西,上面的代码应该可以工作。