如何在javascript中检查是否以及是否在一起

时间:2017-07-05 10:34:07

标签: javascript php html

/ html代码 / 这里有4个最小和最小的文本框。最大的bugdet和区域。我想检查最大值应该大于bugdet和area中的最小区域的值。我在提交表单时检查它。 目前我的条件不适用于其他。

  <div class="col-xs-12 margin-y">
         <div class="width">
            <label class="col-xs-12 col-md-2">Budget</label>
            <div class="col-xs-12 col-md-10">
               <div class="col-xs-12 col-md-6 no-padding-left no-padding-left no-padding-mobile padding-vertical">
                  <input class=" form-control"   placeholder="Minimum Budget"  id="req_min_budget" name="req_min_budget" onkeypress='return validateArea(event);'>
               </div>
               <div class="col-xs-12 col-md-6 no-padding-right no-padding-left no-padding-mobile padding-vertical">
                  <input class=" form-control"  placeholder="Maximum Budget" id="req_max_budget" name="req_max_budget" onkeypress='return validateArea(event);'><span style="color:#d9534f" id="maxBudgetError"></span>
               </div>
            </div>
         </div>
      </div>
      <div class="col-xs-12 margin-y">
         <div class="width">
            <label class="col-md-4 col-md-2">Carpet Area</label>
            <div class="col-xs-12 col-md-10 no-padding padding-vertical">
               <div class="col-md-4  padding-vertical">
                  <input class=" form-control" placeholder="Minimum Area"  onblur="return validateMaxArea1(this.value)"   id="req_min_area" name="req_min_area" onkeypress='return validateArea(event);'>
               </div>
               <div class="col-md-2  no-padding-left  padding-vertical ">
                <select onchange="return changeUnit(this.value)" id="req_carpet_area_unit" required  name="req_carpet_area_unit" class="btn btn-default dropdown-toggle">
         <!-- <option value=''>Select carpet unit</option> -->
                   <?php
         global $SQFT;

          echo $this->home_model->get_dropdown('area_unit',$SQFT)?>
      </select>
               </div>
               <div class="col-md-4 padding-vertical">
                  <input class=" form-control" onblur="return validateMaxArea(this.value)"  placeholder="Maximum Area" id="req_max_area"  name="req_max_area" onkeypress='return validateArea(event);'/><span style="color:#d9534f" id="maxAreaError"></span>
               </div>
                <div class="col-md-2">
                   <select disabled id="req_carpet_area_unit1" required  name="req_carpet_area_unit1" class="btn btn-default dropdown-toggle">
         <!-- <option value=''>Select carpet unit</option> -->
         <?php
         global $SQFT;

          echo $this->home_model->get_dropdown('area_unit',$SQFT)?> 
      </select>
               </div>

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

      <button type="submit"  class="col-xs-offset-8 col-md-offset-11 btn btn-gray" id="req_submit" name="req_submit">Submit</button>

javascript脚本

$(function() {
    // Setup form validation on the #register-form element
    $("#register").validate({
        // Specify the validation rules
        // Specify the validation error messages

        submitHandler: function(form)  {
            var req_min_area = $('#req_min_area').val();
            var req_max_area = $('#req_max_area').val();
            var req_min_budget = $('#req_min_budget').val();
            var req_max_budget = $('#req_max_budget').val();

            if(req_min_area!='' && req_max_area!='') {

                if(parseFloat(req_min_area)>parseFloat(req_max_area)) {
                    $('#req_max_area').css('border-color','#d9534f');
                    document.getElementById("maxAreaError").innerHTML = "Maximum area should be greater than minimum";
                } else {
                    insertReqData();
                }
            } else if(req_min_budget!='' && req_max_budget!='') {
                alert(parseFloat(req_min_budget)>parseFloat(req_max_budget));

                if(parseFloat(req_min_budget)>parseFloat(req_max_budget)) {
                    $('#req_max_budget').css('border-color','#d9534f');
                    document.getElementById("maxBudgetError").innerHTML = "Maximum area should be greater than minimum";
                } else {
                    insertReqData();
                }
            } else {
                insertReqData();
            }
        }
    });
});

2 个答案:

答案 0 :(得分:1)

当你的第一个条件是假的时候,如果你的目标没有成功,那么条件会有效吗

if(req_min_area!='' && req_max_area!='') {
            if(parseFloat(req_min_area)>parseFloat(req_max_area)) {
                $('#req_max_area').css('border-color','#d9534f');
                document.getElementById("maxAreaError").innerHTML = "Maximum area should be greater than minimum";
            } else {
                insertReqData();
            }
        }
        if(req_min_budget!='' && req_max_budget!='') {
            if(parseFloat(req_min_budget)>parseFloat(req_max_budget)) {
                $('#req_max_budget').css('border-color','#d9534f');
                document.getElementById("maxBudgetError").innerHTML = "Maximum area should be greater than minimum";
            } else {
                insertReqData();
            }
        } 

答案 1 :(得分:0)

如果if语句为False(req_min_area或req_max_area为空字符串),则If语句始终为If if elseif。 将你的elseif放在If语句中并将其更改为If if the work。