简体   繁体   English

使用javascript将文本插入输入字段

[英]Insert text in to Input Field using javascript

I am trying to insert text in to an input field using this code . 我试图使用此代码在输入字段中插入文本。

This is the code I am using: 这是我正在使用的代码:

<input class="cid" type="text" name="c1" />
<input class="cid" type="text" name="c2" />
<input type="button" value="Insert Text" onclick="insertAtCaret('c1','TEXTTOINSERT1');insertAtCaret('c2','TEXTTOINSERT2');" />

But nothing is happening. 但是什么也没发生。 I don't know JS so I'm not sure if the JS code I referenced before is only for textareas which did work when I tested. 我不了解JS,所以不确定之前引用的JS代码是否仅适用于在测试时有效的textarea。

Thanks! 谢谢!

The code in the link uses the ID of the element, so you'll have to add an ID : 链接中的代码使用元素的ID,因此您必须添加一个ID:

<input class="cid" type="text" name="c1" id="c1" />
<input type="button" value="Insert Text" onclick="insertAtCaret('c1','TEXTTOINSERT');" />

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM