Html 使用按钮调用javascript函数并从文本框输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19205687/
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
Calling javascript function with button and input from textbox
提问by Sky
I have a textbox and button in my page. I have several sets of the below for different purpose.
我的页面中有一个文本框和按钮。我有几套以下用于不同的目的。
<input type="text" id="new" name="new" value="1" />
<input type="button" value="Edit" onclick="compute(edit(xxx));" />
Upon clicking on the button, I want to call a javascript function which will take in the value input in the textbox. Now I am wondering how am I suppose to retrieve the value from the input 'new' and pass it the 'xxx' as argument?
单击按钮后,我想调用一个 javascript 函数,该函数将接收文本框中输入的值。现在我想知道如何从输入“new”中检索值并将“xxx”作为参数传递给它?
回答by Veer Shrivastav
<input type="button" value="Edit" onClick="compute(edit(document.getElementById('new').value))" />
This is how you handle it there in the HTML.
这就是您在 HTML 中处理它的方式。
Else you can write the same code in javaScript.
否则,您可以在 javaScript 中编写相同的代码。
function edit()
{
x = document.getElementById('new').value;
//use this X.
}
回答by S. S. Rawat
回答by Tamil Selvan C
Try
尝试
<input type="button" value="Edit" onclick="compute(edit(document.getElementById('new').value));" />