• 在表单提交前进行验证的几种方式


    1在button的submit事件时判断

    <button type="submit">提交</button>  
    ("#form").bind("submit",function(){
        var txt_firstname = $.trim($("#firstname").attr("value"));
    
        var isSuccess = 1;//默认验证通过
        if(txt_firstname.length == 0){  
            $("#firstnameLabel").text("firstname不能为空!")  
            $("#firstnameLabel").css({"color":"red"});  
            isSuccess = 0;  //验证不通过,修改isSuccess
       }  
        if(isSuccess == 0){
            return false;//最后未通过,不提交   
        }
    })

    2 在form的onsubmit判断

    <form id="form" method="post" action="/DealWithForm/" onsubmit="return check()">
    function check(){  
        var txt_firstname = $.trim($("#firstname").attr("value"));  
    
        var isSuccess = 1;  
        if(txt_firstname.length == 0)  
        {  
            $("#firstnameLabel").text("firstname不能为空!")  
            $("#firstnameLabel").css({"color":"red"});  
            isSuccess = 0;  
        }  
    
        if(isSuccess == 0){  
            return false;  
        } 
        return true;  
    }  

    注意:onsubmit=“return false”为不执行提交;onsubmit=“return true”或onsubmit=“return ”都执行提交。

    3 去掉submit类型button,直接用普通button.

    <button type="button" onclick="checktosubmit()">提交</button>  
    function checktosubmit(){  
        var txt_firstname = $.trim($("#firstname").attr("value"));
          
        var isSuccess = 1;  
        if(txt_firstname.length == 0)  
        {  
            $("#firstnameLabel").text("firstname不能为空!")  
            $("#firstnameLabel").css({"color":"red"});  
            isSuccess = 0;  
        }  
        if(isSuccess == 1)  
        {  
            form.submit();  
        }  
    }

     原文链接:http://blog.csdn.net/qian_f/article/details/9631691

  • 相关阅读:
    eureka 注册中心(单机版)
    金蝶实际成本培训01
    查看WIN10内核
    金蝶K3 WISE 15.0 GUID
    win10卸载系统自带office365
    金蝶K3wise15.0BOM维护默认只能查看登录账户作为建立人的BOM清单
    阿里云邮箱代收邮件
    金蝶寄售业务流程
    转-商品流通企业代销商品核算方法
    转-ERP待检仓、代管仓、赠品仓
  • 原文地址:https://www.cnblogs.com/alexandra/p/5560048.html
Copyright © 2020-2023  润新知