javascript - 如何在显示警报时尽量减少 if-else 语句

标签 javascript jquery if-statement alert

我正在显示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 statements在 JavaScript 中如果 number of text boxes are 10 or more 。他们有什么解决方案可以最大限度地减少 codeaccordingly 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>

最佳答案

伪代码:

emptyTextboxen = []

for each textboxen as textbox
    if textbox is empty
        emptyTextboxen.push(textbox)

if emptyTextboxen.length == textboxen.length
    message = 'Please fill up all textboxen'
else if emptyTextboxen.length > 0
    message = 'Please fill up textboxen '
    for each emptyTextboxen as emptyTextbox
        message += emptyTextbox + ', '
else
    message = 'Life is good'

关于javascript - 如何在显示警报时尽量减少 if-else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9512944/

相关文章:

javascript if 或语句

java - 如何合并用于在 2D 空间中的单元周围搜索的 if 语句

javascript - jQuery 无法将数组作为 JSON 内容类型发送

javascript - D3 为具有附加父路径和兄弟路径的树结构调整力布局

javascript - 获取属性中最大的元素

javascript - 在框架中将 Javascript 变量传递给 '<a href' 并使用 'window.location.href'

javascript - React-Native:从 Firebase 存储下载图像

jquery - 使 DIV 和 UL 像 SELECT 元素一样

jquery - 在 jQuery 1.7.2 中 ("click") 和 ("click.randomText") 之间有什么区别?

c# - 如何使用 if 条件来添加值