简体   繁体   English

如何在显示警报时最小化if-else语句

[英]how to minimize if-else statements while showing alerts

I am showing alerts if 3 textboxes or any single text box is empty . alerts if 3 textboxes or any single text box is empty我将显示alerts if 3 textboxes or any single text box is empty For example: if 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" similarly if 1st and 3rd text boxes are empty then alert will be "please fill up 1st and 3rd text box" similarly if 2nd and 3rd text boxes are empty then alert will be "please fill up 2nd and 3rd text box" similarly if only 1st text box is empty then alert will be "please fill up 1st text box" similarly if only 2nd text box is empty then alert will be "please fill up 2nd text box" similarly if only 3rd text box is empty then alert will be "please fill up 3rd text box" 例如:如果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" 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 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"

But problem is i have to write so many if-else statements in javascript if number of text boxes are 10 or more . 但是问题是,如果number of text boxes are 10 or more我必须在JavaScript中编写许多if-else statements Is their any solution for this to minimize the code and accordingly alert will come if any of the above condition satisfies ? 他们是否有任何解决方案以最小化code并且accordingly alert will come if any of the above condition satisfies

I have written the if-else code individually but it is very lengthy like this: 我已经单独编写了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>

Pseudocode: 伪代码:

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'

to check every empty checkbox use this simple jquery selector: 要检查每个空的复选框,请使用以下简单的jquery选择器:

jQuery('input.test').not('[value]').each(function() {
    var blankInput = jQuery(this);
    //do what you want with your input
});

your js will be like this: 您的js将是这样的:

function confirmation(domForm) {
    var jForm = jQuery(domForm);
    var values = jForm.find('.check').serializeArray();

    var failedFields = [];
    for(var i = 0; i < values.length; i++) {
        var o = values[i];
        if(o.value == null || o.value.length == 0) {
            failedFields.push(o.name);
        }
    }

    if(failedFields.length > 0) {
        var message = ''; 
        if(failedFields.length == values.length) {
            message = 'fill all fields please';
        }
        else {
            message = 'please fill the fields:';
            for(var i = 0; i < failedFields.length; i++) {
                message += "\n";
                message += failedFields[i];
                }
        }

        alert(message);
        return false;
    }

    return true;
}

call confirmation on your onsubmit! 在您提交时致电确认! like this: 像这样:

<form name="frm" onsubmit="return confirmation(this)" action="confirmsubmit.jsp">
    <input type="text" name="textboxname1" class="check"/>
    <input type="text" name="textboxname2" class="check"/>
    <input type="text" name="textboxname3"/>
    <input type="submit"/> 
 </form>

Maybe this is what you are looking for: 也许这就是您想要的:

FIDDLE 小提琴

$('form[name="frm"]').on('submit', function(e) {
    var empty = $('input[type="text"]', this).filter(function() {
        if ($(this).val()=="") return this;
    });
    if (empty.length > 0) {
        e.preventDefault();            
        if (empty.length == $(this).children('[type="text"]').length) {
            alert('please fill up all');
        }else{
            var str = 'please fill up ';
            $(empty).each(function(index, value) {
                var Sepa = index==empty.length-2 ? ' and ' : ', ',
                    Numb = parseInt(this.name[this.name.length-1]);
                Sepa = index==empty.length-1 ? '' : Sepa;                        
                Numb=Numb==1?'1st':Numb==2?'2nd':Numb==3?'3rd':Numb+'th';
                str=str+Numb+Sepa;
            });
            str=str+' text box';
            alert(str);                
        }   
    }else{
        alert('All are filled, form will be submitted');
    }
});

using jQuery you will be able to do this. 使用jQuery,您将能够做到这一点。 To do this with minimum code I suggest the following approach. 为此,我建议使用以下方法。 Add a class to all the textboxes and if any of them is empty show the alert 向所有文本框添加一个类,如果其中任何一个为空,则显示警报

<input type="text" name="textboxname1" class ="test"/>
<input type="text" name="textboxname2" class ="test"/>
<input type="text" name="textboxname3" class ="test"/>

and in the scripts 并在脚本中

function confirmation() {
   var elements = $('.test');
   var index = 1;

   var message = "";
   elements.each(function(){
       if($(this).val() == ""){
        message += "textbox no" + index + "is empty ";          

       }

   index = index + 1;
   });
  if(message != ""){
   alert(message);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM