Calling javascript function with button and input

2019-09-16 04:06发布

问题:

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));" />