我正在顯示alerts if 3 textboxes or any single text box is empty
。例如:如果 all are empty then alert will be" please fill up all"
else if 1st and 2nd text boxes are empty then alert will be "please fill up 1st and 2nd text box"
同樣if 1st and 3rd text boxes are empty then alert will be "please fill up 1st and 3rd text box"
同樣if 2nd and 3rd text boxes are empty then alert will be "please fill up 2nd and 3rd text box"
同樣if only 1st text box is empty then alert will be "please fill up 1st text box"
同樣if only 2nd text box is empty then alert will be "please fill up 2nd text box"
同樣if only 3rd text box is empty then alert will be "please fill up 3rd text box"
如何在顯示警報時最小化if-else語句
但問題是我是否number of text boxes are 10 or more
寫這麼多if-else statements
在JavaScript。他們的任何解決方案,以儘量減少code
和accordingly alert will come if any of the above condition satisfies
?
我已經單獨寫的if-else code
但這樣很漫長:
<form name="frm" action="confirmsubmit.jsp">
<script type="text/javascript">
<!--
function confirmation() {
var textboxname1=document.frm.textboxname1;
var textboxname2=document.frm.textboxname2;
var textboxname3=document.frm.textboxname3;
//alert if all text boxes are empty
if((textboxname1.value==null)||(textboxname1.value=="")&& (textboxname2.value=="")||(textboxname2.value==null)){
alert("Please fill up first text box<br/>Please fill up second text box<br/>Please fill up 3rd text box");//alert for all
textboxname1.focus();
return false
}
//alert if 2nd text box is empty
else if((textboxname2.value=="")||(textboxname2.value==null))
{
alert("Please Please fill up second text box");//alert for 2nd text box
textboxname2.focus();
return false
}
//alert if 3rd text box is empty
else if((textboxname3.value=="")||(textboxname3.value==null))
{
alert("Please Please fill up third text box");//alert for 3rd text box
textboxname3.focus();
return false
}
// similarly i have to show alert if 2nd and 3rd boxes are empty and so on, but is there any solution to minimize the code?
return true
}
//-->
</script>
<input type="text" name="textboxname1"/>
<input type="text" name="textboxname2"/>
<input type="text" name="textboxname3"/>
<input type="submit" onclick="return confirmation()"/>
</form>
jQuery是允許的嗎? – 2012-03-01 08:37:44
如何將所有值放入數組並在一個循環中檢查這個數組是否爲''或空 – 2012-03-01 08:39:03
警報對話框對於這類事情真的是用戶不友好的。 – 2012-03-01 08:50:43