Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-21-2010, 08:19 PM   PM User | #1
kmk21
New to the CF scene

 
Join Date: Oct 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kmk21 is an unknown quantity at this point
Pulling value from a form with val();

I am having a problem pulling the value from a text input from a form. The line is in bold below.

The jquery:

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

Code:
<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.
kmk21 is offline   Reply With Quote
Old 10-22-2010, 02:11 AM   PM User | #2
Zefris
New Coder

 
Join Date: Jun 2005
Posts: 52
Thanks: 0
Thanked 4 Times in 4 Posts
Zefris is an unknown quantity at this point
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...

Code:
$.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); }); }
}

Last edited by Zefris; 10-22-2010 at 02:20 AM..
Zefris is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:25 AM.


Advertisement
Log in to turn off these ads.