OnSubmit字段中的Html Form If语句

时间:2015-02-24 20:48:26

标签: javascript php html if-statement onsubmit

我对html / php / js相当新,我在有条件地提交表单时遇到了问题。基本上,我试图做的是确认('你想提交这个表单吗?')函数只显示每个字段是否输入了一个值(checkform()函数)。如果两者都是真的,那么表格将提交。非常感谢任何帮助,谢谢!

<script type="text/javascript">
function checkform()
{
	var myForm=document.frmhot;
	
	if(myForm.status.value==""){
		alert("Please select a timeframe status.");	
		return false;
		myForm.status.focus();
	}
    if (myForm.line.value==""){
		alert("Please select a line.");
		return false;
	}
	if(myForm.reason.value==""){
		alert("Please select a reason code.");
		return false;
	}
	if(myForm.partnum.value==""){
		alert("Please enter a part number.");
		return false;
	}
	if(myForm.badgescan.value==""){
		alert("Please enter a badge number.");
		return false;
	}
	return true;
		
}	
</script>
<form method="post" action="newhotpartgenerate.php" name="frmhot" 
onclick="if(checkform();){
	confirm('Do you want to submit the form?');
}
>
         
<input class="button_text" type="submit"  value="Submit" name="submit" onclick= "checkform();"  />
</form>

2 个答案:

答案 0 :(得分:2)

完成工作解决方案,并在一定程度上对IE兼容性进行更正和调整。

<script>
    function checkform(evt)    {        
        var myForm = document.frmhot;
        var condition = true;
        if(myForm.status.value==""){
            alert("Please select a timeframe status."); 
            myForm.status.focus();
            condition = false;
        }
        if (myForm.line.value==""){
            alert("Please select a line.");
            condition = false;
        }
        if(myForm.reason.value==""){
            alert("Please select a reason code.");
            condition = false;
        }
        if(myForm.partnum.value==""){
            alert("Please enter a part number.");
            condition = false;
        }
        if(myForm.badgescan.value==""){
            alert("Please enter a badge number.");
            condition = false;
        }
        if(condition){ condition =  confirm('Do you want to submit the form?'); }

        if(!condition) {
            if(evt.preventDefault) { event.preventDefault(); }    
            else if(evt.returnValue) { evt.returnValue = false; }    
            else { return false; }
        }
    }
</script>
<form method="post" action="newhotpartgenerate.php" name="frmhot" onsubmit="checkform(event)">
    <input type="text" name="status"/>         
    <input type="text" name="line"/>
    <input type="text" name="reason"/>
    <input type="text" name="partnum"/>
    <input type="text" name="badgescan"/>
    <input class="button_text" type="submit" value="Submit"/>
 </form>

答案 1 :(得分:1)

你有正确的想法,只需将你的代码提取到自己的函数中,然后在onclick中调用它。

添加此功能:

function checkAndConfirm() {
     if(checkform()) {
        if (confirm('Do you want to submit the form?')) {
            // submit the form
     }
}

然后从onclick属性中调用它:

<form method="post" action="newhotpartgenerate.php" name="frmhot" onclick="checkAndConfirm()">