日期:2014-05-17  浏览次数:20886 次

JQuery代码不执行
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="henu.BookStoreWeb.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript" src="js/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $("#txtName").blur(function() {
            if ($("#txtName").val() = "") {
                alert("不能为空");
                return false;
            }
            else {
                return true;
            }
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

jquery代码为什么不验证,求各位指点。

------解决方案--------------------
$("#< %= txtName.ClientId%> ").blur(function() {
试试这样。。。
------解决方案--------------------
根据你的页面加载顺序,你在执行js的时候html还没加载
当然找不到txtName这个输入框

你要把你的js包含在

$().ready(function(){
//你的代码
});
------解决方案--------------------
要修改两个地方,刚刚进行了测试,通过了,请注意我这部分js代码:
 <script type="text/javascript">
        $(function() {
            $("#txtName").blur(function() {
                if ($("#txtName").val() == "") {
                    alert("不能为空");
                    return false;
                }
                else {
     &nb