JavaScript验证,然后打印并提交

时间:2016-04-15 11:47:57

标签: javascript printing form-submit

我有一个提交功能来验证表单输入,然后可选(复选框)打印作为提交过程的一部分。

问题是,在打印时,表单提交永远不会完成,而且打印表单提交无法正常工作。

<INPUT class=checkboxes id="Place order" onclick="return checkfields()" type=submit value=SUBMIT name="Place order">

验证始终正常(AFAIK)。

function checkfields() {
    var missinginfo="Please fill the following information";

    var bres = true, qty=0, elem;

    var tqty = document.getElementById('bottles').value;
    if (tqty ==0){alert("No wine selected");bres=false;return bres;}
    if (tqty %6 !=0){
        alert("Orders need to be in 6 bottle packs please add " + (6 -(tqty %6)) + " Bottles to order");
        bres=false;
        return bres;
    }   //end if
    for (i=1; i<30; i++) {
        elem = document.getElementById('f'+i);
        if(elem !=null){
            if(elem.value== ""){   // || 
                //(document.form.website.value.indexOf("http://") == -1) || 
                //(document.form.website.value.indexOf(".") == -1)) {
                bres = false; missinginfo += "\n    " + (document.getElementById('f'+i).name);
            }     //end if
        }     //end if
    }     //end for
    if(!bres){alert (missinginfo );}

    // end of validation here, print if checkbox checked
    if(bres && document.getElementById('cprint').checked==true){window.print();}

    document.getElementById('doc').value = "";
    return bres;
}    //end function

有关如何补救的建议,或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

使用onsubmit代替onclick

<INPUT class="checkboxes" id="Place order"
    onsubmit="return checkfields();"
    type="submit" value="SUBMIT" name="Place order">