admin管理员组

文章数量:1400161

How do you submit text box input to a javascript function without submitting the server?

<input type="text" id="test"  value="" />
<br/>
<button onclick="submitMe(value typed into text box)" id="testButton" >Submit Response</button>

Javascript:

function submitMe(input) {
    alert(input); //should output text box input
}

Thanks

How do you submit text box input to a javascript function without submitting the server?

<input type="text" id="test"  value="" />
<br/>
<button onclick="submitMe(value typed into text box)" id="testButton" >Submit Response</button>

Javascript:

function submitMe(input) {
    alert(input); //should output text box input
}

Thanks

Share Improve this question asked Jan 7, 2014 at 3:31 user3871user3871 12.7k36 gold badges140 silver badges282 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

No need to pass by parameter, just the the element by id.

function submitMe() {
    var value = document.getElementById('test').value;
    alert(value);
}

Or you could pass the id like:

<input type="text" id="test"  value="" />
<br/>
<button onclick="submitMe('test')" id="testButton" >Submit Response</button>

js:

function submitMe(id) {
    var value = document.getElementById(id).value;
    alert(value);
}

Try

<input type="text" id="test"  value="" />
<br/>
<button onclick="submitMe(document.getElementById('test').value)" id="testButton" >Submit Response</button>

DEMO

本文标签: javascriptsubmit text box input using buttonStack Overflow