在警报框中显示消息

时间:2018-07-06 05:58:33

标签: javascript jquery html bootstrap-4

$(document).ready(function(){
$(document).on('click', '#btnSave', function () {
			
			var fname = $("#fname").val();
			var lname = $("#lname").val();
			var email = $("#email").val();
			var mobile = $("#mobile").val();
			
			if(fname == ""){
					alert('First Name is required');
					return false;
			}
			else if(lname == ""){
				alert("Last Name is required");
				return false;
			}
			else if(email == ""){
          alert("Email is required");
					return false;
			}
			
			else(fname == "" && lname == "" && email == ""){
				alert("All Fields are required");
				return false;

			}
			
      
  });
<form id="form1" action=""  method="POST" enctype="multipart/form-data" >

    <fieldset>
    <legend>Basic Info</legend>
    																		
 <div class="row">
     <div class="col-md-6">
       <div class="form-group">
        <label for="name">First Name *</label>
           <input type="text" class="form-control" id="fname" name="fname" value="" placeholder="Enter First Name" data-type="alphanum" maxlength="45"/>
        </div>
    </div>
   <div class="col-md-6">
   	<div class="form-group">
  	  <label for="email">Last Name *</label>
    	<input type="text" class="form-control" id="lname" name="lname" value="" placeholder="Enter Last Name" data-type="alphanum" maxlength="45"/>
   </div>
 </div>										
</div>
    									
 <div class="row">
   <div class="col-md-6">
     <div class="form-group">
    	<label for="name">Email *</label>
     <input type="text" class="form-control" id="email" name="email" value="" placeholder="Enter Email" data-type="alphanum" maxlength="45"/>
     </div>
  </div>
 
<div class="col-md-6">
    <div class="form-group">
    <label for="email">Mobile #</label>
    	<input type="text" class="form-control" id="mobile" name="mobile" value="" placeholder="+55 99 99999999" data-type="alphanum" maxlength="45"/>
   </div>
 </div>										
</div>
    									
 <div class="row" style="display:none;">
    <div class="col-md-6">
    	<div class="form-group">
    	<label for="status">Status</label>
    	<div>
    <span class="text-muted m-l-5">InActive</span>
  <input type="checkbox" data-render="switchery" id="status" name="status" data-theme="default" checked />
    <span class="text-muted m-l-5">Active</span>
    	</div>
   </div>
  </div>
    <div class="col-md-6">
      <div class="form-group">
      <label for="status">All Permissions</label>
     <div>
    <span class="text-muted m-l-5">No</span>
    <input type="checkbox" data-render="switchery" id="status" name="status" data-theme="default" checked />
   <span class="text-muted m-l-5">Yes</span>
    </div>
    </div>									

    </div>									
    </div>	
    </fieldset>	

 <div class="row pull-right">
   <div class="col-md-12">
   <button type="button" class="btn btn-primary m-r-5" id="btnSave" data-loading-text="<i class='fa fa-spinner fa-spin '></i> Process...">Save</button>
<a href="http://localhost/granplay-v3/index.php/managers" class="btn btn-primary">Cancel</a>
    </div>
    </div>
                      
  </form>

if elsif else条件中,else条件不会警告所有字段的消息。当我注释掉最后一个else条件时,它将显示每个字段条件消息。但不是,假设用户未输入任何字段,则显示所有字段的警报消息。 所以我的问题是:为什么其他情况不能使该消息发出警报?

2 个答案:

答案 0 :(得分:3)

如果要先移动其他部分

$(document).ready(function(){
    $(document).on('click', '#btnSave', function () {
        var fname = $("#fname").val();
        var lname = $("#lname").val();
        var email = $("#email").val();
        var mobile = $("#mobile").val();

        if(fname == "" && lname == "" && email == ""){
            alert("All Fields are required");
            return false;
        }else if(fname == ""){
            alert('First Name is required');
            return false;
        }
        else if(lname == ""){
            alert("Last Name is required");
            return false;
        }
        else if(email == ""){
            alert("Email is required");
            return false;
        }else{
            alert("All fields are filled");
            return true;
        }
    });
});

如果所有字段均为空白,则您的第一个条件为true,并且如果其中任何一个已填写,则第一个条件将为错误,因此它将检入另外if-else个部分。

答案 1 :(得分:1)

更改顺序,然后尝试。将条件else放在first中:

if( fname == "" && lname == "" && email == "") {
    alert("All Fields are required");
    return false;

} eles if(fname == ""){
    alert('First Name is required');
    return false;

} else if(lname == ""){
    alert("Last Name is required");
    return false;

} else if(email == ""){
    alert("Email is required");
    return false;
}