我需要要求确认在表单中留下两个字段为空格式的javascript ..这里有例子
Teory
Field1:空白; Field2:空白; Field3:空白;
如果field1在empy中则返回false;要求确认其他2,例如“你确定要离开场地#..空白吗?” >如果按下确定,请检查字段3否则返回假
码
var field1 = document.form.field1.value;
var field2 = document.form.field2.value;
var field3 = document.form.field3.value;
if ((field1 == "") || (field1 == "undefined")) {
alert("Cannot leave field1 blank");
document.form.field1.focus();
return false;
}
else if ((field2 == "") || (field2 == "undefined")) {
var x = confirm("Are U sure to leave field2 blank?");
if (x)
return true;
else
return false;
}
var y = confirm("Are U sure to leave field3 blank?");
if (y)
return true;
else
return false;
}
else {
document.form.submit();
}
当然它不起作用......任何想法? 感谢
答案 0 :(得分:0)
我很快就改写了你的js。我希望它会帮助你。
var form = document.getElementById("myform");
var field1 = form.field1;
var field2 = form.field2;
var field3 = form.field3;
if (form.attachEvent) {
form.attachEvent("submit", formValidation);
} else {
form.addEventListener("submit", formValidation);
}
function formValidation(e) {
if (e.preventDefault) e.preventDefault();
if ((field1.value == "") || (field1.value == "undefined")) {
alert("Cannot leave field1 blank");
field1.focus();
return false;
}
else if ((field2.value == "") || (field2.value == "undefined")) {
var x = confirm("Are U sure to leave field2 blank?");
if (!x) {
field2.focus();
return false;
}
}
else if ((field3.value == "") || (field3.value == "undefined")) {
var y = confirm("Are U sure to leave field3 blank?");
if (!y){
field3.focus();
return false;
}
}
else {
console.log("sending");
form.submit();
}
};
<form action="" id="myform" method="POST">
<input type="text" id="field1" name="field1">
<input type="text" id="field2" name="field2">
<input type="text" id="field3" name="field3">
<input type="submit" value="send">
</form>
答案 1 :(得分:0)
最后我感谢昨天的建议(我再也看不到消息了?!)..
如果有人需要,适用于我的解决方案如下:
$ret = false
if ((field2 == "") || (field2 == "undefined")) {
var x = confirm("Are U sure to leave field2 blank?");
if (x) {
$ret = true;
}
}
if ((field3== "") || (field3== "undefined")) {
if (confirm("Are U sure to leave field3 blank?")){
$retu = true;
}
}
if ( ($ret == true) && ($retu == true) ) {
document.form.submit();
}
感谢所有帮助寻找解决方案的人!