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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:08:54  来源:igfitidea点击:

Calling javascript function with button and input from textbox

javascripthtmlfunctionbuttontextbox

提问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

why you don't try this by Jquery it is to understand and easy to implement.

为什么你不通过 Jquery 尝试这个它是为了理解和易于实现。

Jquery Documentation

Jquery Documentation

$('input[type=button]').click(function(){
alert($('#new').val());// to get input text value
    $('#new').val('xxx');//to set input text value
});

your question Fiddle Here

你的问题小提琴 Here

回答by Tamil Selvan C

Try

尝试

<input type="button" value="Edit" onclick="compute(edit(document.getElementById('new').value));" />