我正在显示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>

最佳答案

要检查每个空的复选框,请使用以下简单的jquery选择器:

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


您的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;
}


在您提交时进行电话确认!像这样:

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

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

10-16 22:19