javascript - no value of textbox captured on first attempt by jquery -
i've simple code
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> </head> <body> <form id="form1" runat="server"> <div> <script> $(document).ready(function () { var username = $("#txtusername").val(); $("#btnsubmit").click(function () { alert(username); }); }) </script> <asp:label id="lblusername" text="user name:" runat="server"></asp:label> <asp:textbox id="txtusername" runat="server"></asp:textbox> <asp:label id="lblpass" text="password:" runat="server"></asp:label> <asp:textbox id="txtpass" runat="server"></asp:textbox> <asp:button id="btnsubmit" text="submit" runat="server" /> </div> </form> </body> </html>
when run in browser, provide value textbox , click on button alerts blank. when clicked next time shows value. issue.?
2nd attempt.
that's because reading value outside event handler. want read value of textbox when user clicks on submit button, not before it.
it written follows:
$(document).ready(function() { // code executed when page loads. $("#btnsubmit").click(function() { // code executed when user clicks on submit button. var username = $("#txtusername").val(); alert(username); }); })
Comments
Post a Comment