1
$(document).ready(function(){
    $('.submit').click(function(){
         var value = $('#Name').attr('value');
        alert(value); 
    });  
});
<input type="text" value="10" id="Name"/>Hello world
<input type="submit" class="submit" value="submit"/>

Result is "Hello World", I want get value is "10", how to fix it ?

1
  • It gives 10 on my computer. Which JQuery version are you using? Commented Jan 18, 2012 at 4:57

2 Answers 2

3

I'm not sure why you're getting Hello World, but to retrieve an input's value, use the val() function

var value = $('#Name').val();

You don't have a second input on your page anywhere with an id of "Name", do you?


Also, depending on what you want to do, you may be better off handler the form's submit event, rather than the submit button's click handler:

$(document).ready(function(){
    $('form').submit(function(){
         var value = $('#Name').val();
        alert(value); 
    });  
});
Sign up to request clarification or add additional context in comments.

2 Comments

Don't know why everyone use attr for value today
Don't know why they submit forms using click event on a button!
0

Working fiddle of example,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.