Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    52
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •