使用按钮和来自文本框的输入调用 javascript 函数

Calling javascript function with button and input from textbox

本文关键字:输入 调用 javascript 函数 文本 按钮      更新时间:2023-09-26

我的页面中有一个文本框和按钮。我有几套以下用于不同目的。

<input type="text" id="new" name="new" value="1" />
<input type="button" value="Edit" onclick="compute(edit(xxx));" />

单击按钮后,我想调用一个 javascript 函数,该函数将接收文本框中的值输入。现在我想知道我应该如何从输入"new"中检索值并将其作为参数传递给它?

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

这就是您在 HTML 中处理它的方式。

否则,你可以在javaScript中编写相同的代码。

function edit()
{
     x = document.getElementById('new').value;
     //use this X.
}
为什么

你不尝试Jquery,它是易于理解和实现的。

Jquery Documentation

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

你的问题小提琴Here

尝试

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