...

View Full Version : Pulling value from a form with val();



kmk21
10-21-2010, 08:19 PM
I am having a problem pulling the value from a text input from a form. The line is in bold below.

The jquery:



$(document).ready(function( numberInput ) {

var numberInput = $("#numberInput").val();

senddata = function() {

$.post("post.php", { number: numberInput }, function(data) {
$("#results").replaceWith(data);
});
}

});


The Form:



<form name="form">
<input id="numberInput" type="text">
<input onclick="senddata();" type="button" value="send data">
</form>


Anyone have any ideas?? Help would be much appreciated.

Zefris
10-22-2010, 02:11 AM
Your $.post statement is inside a closure, but has a reference to the numberInput variable. Since numberInput is only set once when the document has been loaded (which would be a blank string), you'll always be getting the same value when you click the button.

Any of the 2 below should fix it...


$.post("post.php", { number: $("#numberInput").val() }, function(data) { $("#results").replaceWith(data); }); }

or

senddata = function() {
var numberInput = $("#numberInput").val();
$.post("post.php", { number: numberInput }, function(data) { $("#results").replaceWith(data); }); }
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum