验证后提交Javascript表单 [英] Javascript Form Submission After Validation

查看:89
本文介绍了验证后提交Javascript表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该脚本主要发挥我的作用:当未选中单选框时发出警报.但是,如果选择了所有按钮,则需要提交表单...这就是我挂断的地方.现在,如果所有字段和按钮都被选中,那么我仍然会收到带有var alertMsg的警报.有什么想法吗?

This script functions mostly how I would like it to: alert when a radio checkbox has not been selected. However, if all buttons are selected I need it the form to be submitted...thats where I'm hung up. Right now if all fields and buttons are selected then I still get an alert with var alertMsg. Any ideas?

function submitform() {
    var sizeChoice = ""
    var size = document.store.on1.length
    var fontChoice = ""
    var len = document.store.on2.length
    var materialChoice = ""
    var material = document.store.on3.length
    var treatmentChoice = ""
    var treatment = document.store.on4.length
    var a = document.forms["store"]["item_name"].value;
    var alertMsg = "Please Choose a:"
    for(i = 0; i < size; i++) {
        if(document.store.on1[i].checked) {
            sizeChoice = document.store.on1[i].value
        }
    }
    for(i = 0; i < len; i++) {
        if(document.store.on2[i].checked) {
            fontChoice = document.store.on2[i].value
        }
    }
    for(i = 0; i < material; i++) {
        if(document.store.on3[i].checked) {
            materialChoice = document.store.on3[i].value
        }
    }
    for(i = 0; i < treatment; i++) {
        if(document.store.on4[i].checked) {
            treatmentChoice = document.store.on4[i].value
        }
    }
    if(a == null || a == "") alertMsg += "\n" + "Name" + "\n";
    if(sizeChoice == "") {
        alertMsg += "Size" + "\n"
    }
    if(fontChoice == "") {
        alertMsg += "Font" + "\n"
    }
    if(materialChoice == "") {
        alertMsg += "Material" + "\n"
    }
    if(treatmentChoice == "") {
        alertMsg += "Treatment" + "\n"
    } {
        alert(alertMsg)
    };
    return false;
    document.forms["form"].submit();
};

推荐答案

无论您进行的验证如何,您都将返回false.从以下位置更改代码的结尾:

You're returning false regardless of your validation. Change the end of your code from:

if(treatmentChoice == "") {
    alertMsg += "Treatment" + "\n"
} {
      alert(alertMsg)
};
return false;
document.forms["form"].submit();

收件人:

if(treatmentChoice == "") {
    alertMsg += "Treatment" + "\n"
}
if(alertMsg.length > 16) {
    alert(alertMsg);
    return false;
} else {
    document.forms["form"].submit();
}

长度检查对照您最初设置的内容来检查alertMsg的最终值长度.

The length check checks the final value length of alertMsg against what you originally set it to.

这篇关于验证后提交Javascript表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆