当前位置:编程学习 > C#/ASP.NET >>

实现jquery校验

本人菜鸟一个,希望大神多多指教,最好能把代码下来,谢谢各位了!!!!




 <script src="../Script/jquery-1.8.3.js" type="text/javascript"></script>
    <script type="text/javascript">
        function checkName() {
            var name = document.getElementById("txtUserName").value;
            if (name == "") {
                alert("请输入用户名");
           }else{

                 alert("用户名可用!!!!");
           }
        }
    </script>

这样写的没有效果,请各位高手多多指点。




 <table cellspacing="0" cellpadding="0" width="100%" border="0" style="text-align: center">
            <tr>
                <td class="tdbg">
                    <table cellspacing="0" cellpadding="0" width="100%" border="0">
                        <tr>
                            <td height="25" width="30%" align="right">
                                用户名 :
                            </td>
                            <td height="25" width="*" align="left">
                                <asp:TextBox ID="txtUserName" runat="server" Width="200px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td height="25" width="30%" align="right">
                                密码 :
                            </td>
                            <td height="25" width="*" align="left">
                                <asp:TextBox ID="txtUserPwd" runat="server" Width="200px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td height="25" width="30%" align="right">
                                电话 :
                            </td>
                            <td height="25" width="*" align="left">
                                <asp:TextBox ID="txtUserPone" runat="server" Width="200px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td height="25" width="30%" align="right">
                                邮箱 :
                            </td>
                            <td height="25" width="*" align="left">
                                <asp:TextBox ID="txtUserEmail" runat="server" Width="200px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td height="25" width="30%" align="right">
                                上级 :
                            </td>
                            <td height="25" width="*" align="left">
                                <asp:DropDownList ID="DropDownListUpsID" runat="server">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td height="25" width="30%" align="right">
                                部门 :
                            </td>
                            <td height="25" width="*" align="left" valign="bottom">
                                <asp:DropDownList ID="DropDownDeptID" runat="server">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td height="25" width="30%" align="right">
                                职位 :
                            </td>
                            <td height="25" width="*" align="left">
                                <asp:DropDownList ID="DropDownPositionID" runat="server">
                                </asp:DropDownList>
                            </td>
                        </tr>
                    </table> --------------------编程问答-------------------- jq里的ajax方法 --------------------编程问答-------------------- 没看到你那里用了checkName这个方法 --------------------编程问答-------------------- 既然用了jquery,这句可以这样写吧:

var name = document.getElementById("txtUserName").value;
var name = $("#txtUserName").val(); --------------------编程问答-------------------- 哥们  到底是jquery还是js  --------------------编程问答-------------------- <asp:TextBox ID="txtUserName" runat="server" Width="200px"></asp:TextBox>注意这个事服务器控件,获取ID的方式是<%=txtUserName.ClientID%> --------------------编程问答--------------------
引用 5 楼 shiyong7682719 的回复:
<asp:TextBox ID="txtUserName" runat="server" Width="200px"></asp:TextBox>注意这个事服务器控件,获取ID的方式是<%=txtUserName.ClientID%>


如果加了母版就这样:var name = $("#<%=txtUserName.ClientID%>").val(); 

没有的话可以这样:var name = $("#txtUserName").val(); 

<script type="text/javascript">
         function checkName() {
             var name = $("#txtUserName").val(); 
             if (name == "") {
                 alert("请输入用户名");
            }else{
 
                 alert("用户名可用!!!!");
            }
         }
     </script>
  --------------------编程问答-------------------- function 木调用啊。 --------------------编程问答--------------------  document.getElementById("txtUserName").value;
不能直接调用服务器控件的value值
可以用clintId
或者在控件上添加ClientIDMode="Static"属性
而且 你还没调用这个方法
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,