今天在做一个生成订单的简单验证页面

主要功能:实现点击提交按钮后,先在页面上用JavaScript验证信息是否输入完全,不完全不允许提交,待输入完全后,才可以提交

页面:如下页面,可以看到,当文本框有未填值的项,点击底部“去结算”按钮,文本框后面的图片会显示,提示有错,并且“去结算”按钮旁边也显示一段文字,提示有内容没有填写,不难看出,红叉图片和提示文字是同时存在的

但是这张图片里就出现了问题,明明填写了内容,并且红叉图片也没有了,但是底部提示文字还在,因为第一次点击“去结算”按钮,故意验证失败,再次点击“去结算”按钮,就出现了问题,如果是第一次点击“去结算”按钮,就直接验证成功,那么也不会出现这个问题

代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Cart.Master" AutoEventWireup="true" CodeBehind="OrderConfirm.aspx.cs" Inherits="Web.OrderConfirm" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="JS/jquery-1.4.2.js"></script>
<style type="text/css">
ul {
margin-left: 100px;
list-style-type: none;
margin-bottom: 0px;
} ul li {
margin-bottom: 6px;
} ul input {
width: 300px;
} #orders td {
border: 2px solid #cbcbcb;
}
</style>
<script type="text/javascript">
//验证数据是否有效的方法
var isValidate = true; //定义一个全局变量,表示数据验证是否成功,默认为true
function Validate()
{
//关键步骤,因为如果第一次验证失败,isValidate已经被赋值为false
        //这里如果不修改为true,那么OnSubmit方法里调用这个变量的话就一直是false,会导致验证不成功
       isValidate = true; //获得所有class=txtText的input
$(".txtText").each(function () {
if ($(this).val() == "") {
//如果这个input的value为空,则显示其后面的图片
$(this).next().attr("style", "display:inline");
isValidate = false;
}
else {
//如果这个input的value不为空,则隐藏其后面的图片
$(this).next().attr("style", "display:none");
} });
} function OnSubmit()
{
Validate();
if (isValidate == false) {
$("#txtMsg").text("请检查收货信息是否填写完整!");
return false;
}
} </script>
</asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table style="width: 80%; margin: 0px auto; border: 5px solid #64ccfa; border-collapse: collapse;">
<tr>
<td style="height: 20px; background-color: #64ccfa;">
<p style="font-weight: bold; margin-left: 20px;">填写收货信息并确认订单</p>
</td>
</tr>
<tr>
<!--收货人信息:四个文本框,以及表示内容是否有效的图片-->
<td style="border-bottom: 2px dotted gray; padding-bottom: 10px;">
<p style="font-weight: bold; margin-left: 25px; margin-top: 5px;">收货人信息:</p>
<ul>
<li>姓名:<input type="text" class="txtText" name="txtName" value="<%=currUser.Name %>" /><img src="Images/cha.ico" style="display:none;" width="" height="" /></li>
<li>地址:<input type="text" class="txtText" name="txtAddress" value="<%=currUser.Address %>" /><img src="Images/cha.ico" style="display:none;" width="" height:"" /></li>
<li>电话:<input type="text" class="txtText" name="txtPhone" value="<%=currUser.Phone %>" /><img src="Images/cha.ico" style="display:none;" width="" height:"" /></li>
<li>邮编:<input type="text" class="txtText" name="txtCode" /><img src="Images/cha.ico" style="display:none;" width="" height:"" /></li>
</ul>
</td>
</tr>
<tr>
<!--请选择支付方式:支付宝和在线支付两种-->
<td style="border-bottom: 2px dotted gray; padding-bottom: 10px;">
<p style="font-weight: bold; margin-left: 20px; margin-top: 5px;">请选择支付方式:</p>
<span style="margin-left: 25px; height: 10px;">支付方式:</span>
<img src="Images/y_zfb.gif" alt="支付宝" style="vertical-align: middle;" />
<input type="radio" name="payWay" value="支付宝" checked="checked" />
<img src="Images/unionpay.gif" alt="在线支付" style="vertical-align: middle; margin-left: 50px;" />
<input type="radio" name="payWay" value="在线支付" />
</td>
</tr>
<tr>
<!--用 Repeater 控件,绑定数据-->
<td style="padding-bottom: 15px;">
<p style="font-weight: bold; margin-left: 20px; margin-top: 5px;">商品清单:</p>
<!--订单内容-->
<div id="orders">
<table style="width: 95%; margin: 0px auto; border-collapse: collapse; border: 2px solid #cbcbcb; text-align: center;">
<asp:Repeater ID="rpsCart" runat="server">
<HeaderTemplate>
<tr style="font-weight: bold; background-color: #64ccfa;">
<td style="width: 20%;">图书编号</td>
<td style="width: 50%;">商品名称</td>
<td style="width: 15%;">单价</td>
<td style="width: 15%;">数量</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="orderList">
<td><%#Eval("Book.Id") %></td>
<td style="text-align: left;"><a href="BookDetail.aspx?<%#Eval("Book.Id") %>"><%#Eval("Book.Title") %></a></td>
<td style="color: red; font-weight: bold;"><span id="unitPrice"><%#Eval("Book.UnitPrice","{0:0.00}") %></span></td>
<td><span id="count"><%#Eval("Count") %></span></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
<!--显示总价格,提示消息,以及提交按钮-->
<div style="text-align: right;">
<span style="font-weight: bold;">你需要支付的总金额为:<span style="color: red; font-weight: bold;">¥</span></span><span style="color: red; font-weight: bold; margin-right: 25px;" id="totalPrice"><%=totalPrice %></span><br />
<span id="txtMsg" style="color:red;font-weight:bold;"></span>
<input type="submit" style="background-image: url(Images/gopay.jpg); width: 94px; height: 32px; margin-right: 30px; margin-top: 10px;" value="" name="btnGoPay" onclick="return OnSubmit()" />
</div>
</td>
</tr>
</table>
</asp:Content>

关键:1,在type=“submit” 的按钮,的时候,设置 onclick=“return OnSubmit()”

    因为要先验证数据,在OnSubmit()方法里,调用验证数据的方法 Validate() ,验证不成功,返回false

  2,在Validate()方法里,对全局变量 isValidate 要进行付初始值 true

    因为第一次验证失败,会给 isValidate 赋值为false,如果在 Validate() 方法里不给其赋值为 true ,那么,之后再次验证,尽管验证成功,isValidate 的值还是 false,    而在OnSubmit()方法里是通过判断全局变量 isValidate 的值来判断是否验证成功,那么就会导致 OnSubmit() 方法判断的值一直是false,一直不成功,导致图片2的错    误

心得:这是个细节问题,有的时候考虑不全面,就很容易导致这种问题,需要代码一行行的调试,才发现,原来是错在自己不细心---出错的永远是人

关于 submit 按钮的 onclick 验证事件,第一次验证失败,第二次 submit 按钮失效的原因解析的更多相关文章

  1. 【C#】让工具栏ToolStrip能触发焦点控件的Leave、Validating、DataError等事件以验证数据

    ----------------更新:2014-04-21--------------- 蒙doggo兄指教,得知有更好的方法可以代替蹩脚的0尺寸Button法,即调用窗体的验证方法Form.Vali ...

  2. 工具栏ToolStrip能触发焦点控件的Leave、Validating、DataError等事件以验证数据 z

    public class ToolStripEx : ToolStrip { protected override void OnClick(EventArgs e) { base.OnClick(e ...

  3. button 按钮,结合onclick事件,验证和提交表单

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

  4. 【转】监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类

    http://lib.csdn.net/article/unity3d/38463 作者:IceFantasyLcj 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的第 ...

  5. 【转】 NGUI 监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类

    http://blog.csdn.net/icefantasylcj/article/details/49450555 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的 ...

  6. HTML 表单和验证事件

    1.表单验证<form></form> (1)非空验证(去空格) (2)对比验证(跟一个值对比) (3)范围验证(根据一个范围进行判断) (4)固定格式验证:电话号码,身份证号 ...

  7. 表单和验证事件以及marquee标签

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

  8. HTML--9表单和验证事件

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

  9. HTML表单和验证事件

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

随机推荐

  1. Git对于单个文件的分批提交方式的使用

    很多时候,对于一个大的文件,可能有的同学改完之后不想一次提交,想分批提交.但这个时候由于git add的机制往往add之后就是整个一个文件被放到stage区了,这个时候肯定会想能不能对一个文件可以进行 ...

  2. Python标准库--os模块

    这个模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行.一个例 ...

  3. Apache下PHP的几种工作方式

    PHP在Apache中一共有三种工作方式:CGI模式.Apache模块DLL.FastCGI模式. 一.CGI模式 PHP 在 Apache 2中的 CGI模式.编辑Apache 配置文件httpd. ...

  4. LightOJ 1317 第六周比赛A题

    A - A Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description Y ...

  5. 读取word文件.选择了TextParse

    待续! 代码还没分离出来.. 分离后会上传上来 不支持wps 文件 . ]]>

  6. 前端工程之模块化(来自百度FEX)

    模块化 是一种处理复杂系统分解成为更好的可管理模块的方式,它可以把系统代码划分为一系列职责单一,高度解耦且可替换的模块,系统中某一部分的变化将如何影响其它部分就会变得显而易见,系统的可维护性更加简单易 ...

  7. Android HttpClient POST JSON Restful-web-services

    @Override protected String doInBackground(String... arg0) { Gson gson = new Gson(); String json = gs ...

  8. 关于TFS2012无法发送警报邮件的问题

    前几天把公司的TFS从2010升级到2012,整个过程经历了3个小时,过程比较顺利.升级完成后眼馋各种新功能,感叹知识真是待到用时方恨少呐,只得一个一个去摸索了.其中有一个“警报”的功能让我印象深刻( ...

  9. HDOJ(HDU) 2103 Family planning(需要注意范围)

    Problem Description As far as we known,there are so many people in this world,expecially in china.Bu ...

  10. jquery 异步请求Demo【转载】

    $(document).ready(function() { $.ajax({ url : "/AjaxAction/LoginArea.ashx", data : {userna ...