用户单击按钮时,在输入字段中添加一串文本 [英] Add a string of text into an input field when user clicks a button
本文介绍了用户单击按钮时,在输入字段中添加一串文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上只是尝试将文本添加到已经包含值的输入字段中..触发器是一个按钮..
Basically just trying to add text to an input field that already contains a value.. the trigger being a button..
在我们点击按钮之前,表单字段会看起来像...(用户输入了一些数据)
Before we click button, form field would look like.. (user inputted some data)
[This is some text]
(Button)
单击按钮后,字段看起来像..(点击$后我们添加 c $ c>到当前值)
After clicking button, field would look like.. (we add after clicking
to the current value)
[This is some text after clicking]
(Button)
尝试仅使用javascript完成..
Trying to accomplish using javascript only..
推荐答案
你工作的例子
HTML:
<input type="text" value="This is some text" id="text" style="width: 150px;" />
<br />
<input type="button" value="Click Me" id="button" />
jQuery:
<script type="text/javascript">
$(function () {
$('#button').on('click', function () {
var text = $('#text');
text.val(text.val() + ' after clicking');
});
});
<script>
Javascript
<script type="text/javascript">
document.getElementById("button").addEventListener('click', function () {
var text = document.getElementById('text');
text.value += ' after clicking';
});
</script>
工作jQuery示例: http://jsfiddle.net/geMtZ/
Working jQuery example: http://jsfiddle.net/geMtZ/
这篇关于用户单击按钮时,在输入字段中添加一串文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文