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,因为在下面的示例中它可以完美地工作。



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" />

09-28 05:22