javascript 如何通过单击按钮向文本框添加文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21094727/
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-10-27 20:02:32  来源:igfitidea点击:

How to add text to a textbox by clicking a button

javascripthtmlforms

提问by Samp

I would like to have a button to the right of a textbox which when clicked would populate the textbox with a message. I am assuming I can accomplish this with Javascript but the "onclick" method doesn't seem to work for me. I am probably doing it wrong.

我想在文本框右侧有一个按钮,单击该按钮时会在文本框内填充一条消息。我假设我可以使用 Javascript 完成此操作,但“onclick”方法似乎对我不起作用。我可能做错了。

采纳答案by Bhavesh Kachhadiya

Using JavaScript and HTML :

使用 JavaScript 和 HTML :

I am guessing what you need is, may be as follow:

我猜你需要的是什么,可能如下:

<!DOCTYPE html>
<html>
<head>
<script>
function ButtonClick_Test()
{
document.getElementById("result").value= ' your text here';
}
</script>
</head>

<body>
<form>
Click Here:<br/>
<input type="button" value="Click Here" name="no" onclick="ButtonClick_Test()"> 

<input type="text" id="result" size="20">
</form>
</body>
</html>

回答by Petar Vasilev

Assuming you are using jQuery(http://jquery.com/):

假设您使用的是 jQuery(http://jquery.com/):

$('#buttonID').click(function() {
    $('#textareaID').val('Your text goes here');
}