【问题标题】:jQuery : input type text value is showing in alertjQuery:输入类型文本值显示在警报中
【发布时间】:2016-04-13 08:39:07
【问题描述】:

 function ValidateLogin() {
   alert($("#txtUserName").val());
   //alert($('#txtUserName').val());
 }
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Login | Profi Admin</title>
  <link href="css/960.css" rel="stylesheet" type="text/css" media="all" />
  <link href="css/reset.css" rel="stylesheet" type="text/css" media="all" />
  <link href="css/text.css" rel="stylesheet" type="text/css" media="all" />
  <link href="css/login.css" rel="stylesheet" type="text/css" media="all" />
</head>

<body>
  <div class="container_16">
    <div class="grid_6 prefix_5 suffix_5">
      <h1>Login</h1>
      <div id="login">
        @*
        <p class="tip">You just need to hit the button and you're in!</p>
        <p class="error">This is when something is wrong!</p>*@
        <form id="form1" name="form1" method="post" action="">
          <p>
            <label>
              <strong>Username</strong> 
              <input type="text" name="UserName" class="inputText" id="txtUserName" />
            </label>
          </p>
          <p>
            <label>
              <strong>Password</strong>
              <input type="password" name="Password" class="inputText" id="txtPassword" />
            </label>
          </p>
          <a class="black_button" onclick="return ValidateLogin();"><span>Authentification</span></a>
          <label>
            <input type="checkbox" name="checkbox" id="checkbox" />Remember me</label>
        </form>
        <br clear="all" />
      </div>
      <div id="forgot">
        <a href="#" class="forgotlink"><span>Forgot your username or password?</span></a>
      </div>
    </div>
  </div>
  <br clear="all" />
</body>

</html>

在这里,在警报中,我没有得到用户名。我也试过用单引号,还是没有显示出来。

【问题讨论】:

  • 你还没有添加对jquery文件的引用。
  • 或添加像jsfiddle.net/6jyzyeq4这样的事件处理程序

标签: javascript jquery text input


【解决方案1】:

也许您忘记在您的网站中启用 Jquery,因为在下面的示例中它运行良好。

function ValidateLogin() {
       alert($("#txtUserName").val());
       //alert($('#txtUserName').val());
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container_16">
    <div class="grid_6 prefix_5 suffix_5">
        <h1>Login</h1>
        <div id="login">
            
            <form id="form1" name="form1" method="post" action="">
                <p>
                    <label>
                        <strong>Username</strong>                            
                        <input type="text" name="UserName" class="inputText" id="txtUserName" />
                    </label>
                </p>
                <p>
                    <label>
                        <strong>Password</strong>
                        <input type="password" name="Password" class="inputText" id="txtPassword" />
                    </label>
                </p>
                <a class="black_button" onclick="return ValidateLogin();"><span>Authentification</span></a>
                <label>
                    <input type="checkbox" name="checkbox" id="checkbox" />
                    Remember me</label>
            </form>
            <br clear="all" />
           </div>
           <div id="forgot">
            <a href="#" class="forgotlink"><span>Forgot your username or password?</span></a>
           </div>
        </div>
     </div>
    <br clear="all" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多