gpt4 book ai didi

javascript - OnSubmit 字段中的 Html 表单 If 语句

转载 作者:行者123 更新时间:2023-11-28 19:21:50 25 4
gpt4 key购买 nike

我对 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>

最佳答案

完整的工作解决方案,并在一定程度上对 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>

关于javascript - OnSubmit 字段中的 Html 表单 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28705942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com