Calling javascript function with button and input from textbox

点点圈 提交于 2019-12-13 06:29:28

问题


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?


回答1:


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

This is how you handle it there in the HTML.

Else you can write the same code in javaScript.

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



回答2:


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

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




回答3:


Try

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


来源:https://stackoverflow.com/questions/19205687/calling-javascript-function-with-button-and-input-from-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!