Javascript 如何在表单输入中添加文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6714564/
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
How to add text to form input
提问by Brandon
I am really not sure where to start with this. I have a form input box and submit button. When the submit button is pressed, the form content is submitted and used in some javascript. What I want to do is automatically add some text to the end of the submission.
我真的不知道从哪里开始。我有一个表单输入框和提交按钮。当提交按钮被按下时,表单内容被提交并在一些 javascript 中使用。我想要做的是在提交的末尾自动添加一些文本。
So if a user inputs "The dog walked" and pressed submit, the form would add "across the street." to the end of the submission.
因此,如果用户输入“The dog walk”并按下提交,表单将添加“在街对面”。到提交结束。
Thank you!!
谢谢!!
回答by Dan Grossman
In your event listener for the form's submit action, change the input.
在表单提交操作的事件侦听器中,更改输入。
document.getElementById('theinputid').value = document.getElementById('theinputid').value + "across the street."
回答by Sascha Galley
W3 schools is your friend: Form onsubmit Event
So in your case it's something similar to:
W3 学校是你的朋友:Form onsubmit Event
所以在你的情况下它类似于:
<script type="text/javascript">
function addText() {
var input = document.getElementById('something');
input.value = input.value +' across the street';
}
</script>
<form name="frm1" action="?" onsubmit="addText()">
<input type="text" name="somehting" id="something" />
<input type="submit" value="Submit" />
</form>
回答by Ouchane CC
You Can Use +=
, for instance:
你可以使用+=
,例如:
input.value +=