mardi 4 mars 2014

jQuery: How to get text in a control if value attribute is undefined?


Vote count:

0




I have a simple account number and password form. I can submit the form in a function that serializes the form and sends it to a Web API controller, and the account number and password are received as properties in a small model I created. So that works fine. But I wanted to access the number that the user typed on the page itself, but I cannot figure out how to do it - I tried using var accountNo = $('#account').val() but accountNo is undefined.


All the examples that I have found have the 'value' attribute set in the HTML, wither hardcoded or set using .val('someValue'). But it works to have the user enter the text and then serializing the form, leaving the value attribute empty. So how do I get the text that the user entered?


I put a sample on jsFiddle: http://ift.tt/1f1Rk03



<input name="account" type="text" placeholder="Enter Account Number">


and



$(function() {
var accountNo = $('#account').val();
console.log('Account: ' + accountNo);
});


asked 47 secs ago

Pete

13





Aucun commentaire:

Enregistrer un commentaire