JQuery:无法检索用户名和密码

时间:2015-05-18 20:02:47

标签: jquery

由于某种原因,我似乎无法获得正在输入的用户名和密码。这是我的代码:

// HTML

<div class="loginForm"> 
<div id="username">
  <input type="email" placeholder="email address"  required />
</div>

 <div id="password">
    <input type="password" placeholder="password" required />
 </div>

 <div id="loginButton"><div id="loginText"><span>Login</span></div></div>

的javascript

$("#loginButton").click(function(){
var usr = $("#username").val();
var password = $("#password").val();
alert("user=" + usr + " and password=" + password);
});

非常直接,但出于某种原因,它总是空白。

我在这里创建了一个jsfiddle:http://jsfiddle.net/pr1yLdLt/2/

3 个答案:

答案 0 :(得分:1)

你需要这样做:

Math.sqrt(parseFloat($scope.console)).toString();

答案 1 :(得分:1)

您应该将ID设为div而不是<div class="loginForm"> <div > <input id="username" type="email" placeholder="email address" required /> </div> <div > <input id="password" type="password" placeholder="password" required /> </div> <div id="loginButton"><div id="loginText"><span>Login To Meecal</span></div></div>

{{1}}

答案 2 :(得分:1)


var usr = $("#username").val();

它指向一个div,一个id =&#34;用户名&#34;的div,你需要指向<input>所以写下这个:

<div class="loginForm">
    <div>    
        <input id="username" name="username" type="email" placeholder="email address"  required />  
    </div> 
    <div> 
        <input id="password" name="password" type="password" placeholder="password" required />   
    </div> 
    <div id="loginButton"><div id="loginText"><span>Login To Meecal</span></div>
</div>