在提交表单之前确认消息

时间:2015-10-09 09:00:58

标签: javascript onsubmit

我正在验证表格,然后通过javascript要求保密......  所以提交我已经调用了两个函数名称validate()& make_confirm()..

onsubmit="return(validate() && show_alert(this));"

通过这个我部分能够调用这两个功能但确认部分工作不正常我必须将其重定向到另一个页面,同时按下确定但它不会去帮助我

验证& make_sure()函数如下:

function validate() {
           if(document.getElementById('cname').value == '')
            {
                alert('Please enter your name');
                document.getElementById('cname').focus();
                return false;
            }
            else if(document.getElementById('address').value == '')
            {
                alert('Please enter your address');
                document.getElementById('address').focus();
                return false;
            }
            else if(document.getElementById('city').value == '')
            {
                alert('Please choose your city');
                document.getElementById('city').focus();
                return false;
            }
            else if(document.getElementById('state').value == '')
            {
                alert('Please enter your state');
                document.getElementById('state').focus();
                return false;
            }
function make_sure() {
          if(confirm("Do you really want to do this?"))
            document.forms[0].submit();
          else
            return false;
        }

1 个答案:

答案 0 :(得分:0)

使用一个功能进行验证并确认并设置表单的action以将表单当前页面重定向到另一个页面。

function validateAndConfirm() {
    if( isEmpty(id('cname').value) ) {
        alert('Please enter your name');
        id('cname').focus();
        return false;
    }
    else if( isEmpty(id('address').value) ) {
        alert('Please enter your address');
        id('address').focus();
        return false;
    }
    else if( isEmpty(id('city').value) ) {
        alert('Please choose your city');
        document.getElementById('city').focus();
        return false;
    }
    else if( isEmpty(id('state').value) ) {
        alert('Please enter your state');
        id('state').focus();
        return false;
    } else {
        if(confirm("Do you really want to do this?")) {
            document.forms[0].submit();
        }
        else {
            return false;
        }
    }
}

function isEmpty(val){
    return (typeof val == 'undefined' || val === undefined || val == null || val.length <= 0) ? true : false;
}

// this function help to simplify you writing : document.getElementById to just id
function id(sid){
   return document.getElementById(sid);
}