Javascript:自动获取if else函数

时间:2017-05-05 07:06:37

标签: javascript php html

我想根据他的in-timeout-time获得一个人工作的天数。当它从表格行中选出并且它不能使用我的js代码时,它应该自动计算日期字段

HTML

<fieldset> In-Time:
   <input class="input" type="text" name="InTime" id="intime" value="" />
</fieldset>
<fieldset>Out-Time:
    <input class="input" type="text" name="OutTime" id="outtime" value="" />
</fieldset>
<fieldset> Day:
   <input class="input" type="text" id="day" name="Day" value="" />
</fieldset>

JQUERY

$(document).ready(function (){
  $('#intime').on('input', function() {
    if (intime == 08:00:00 && outtime >= 18:00:00) {
      day == 1;
    }else if (intime == 08:00:00 && outtime == 12:30:00) {
      day == 0.5;
    }else (intime == 12:30:00 && outtime >= 18:00:00) {
      day == 0.5;
    };
  });
  $('#outtime').on('input', function() {
    if (intime == 08:00:00 && outtime >= 18:00:00) {
      day == 1;
    }else if (intime == 08:00:00 && outtime == 12:30:00) {
      day == 0.5;
    }else (intime == 12:30:00 && outtime >= 18:00:00) {
      day == 0.5;
    };
  });
});

enter image description here

3 个答案:

答案 0 :(得分:0)

编写脚本如下: -

$(document).ready(function (){
    $('#intime,#outtime').on('input', function() {
        // declare variables
        var intime = $("#intime").val() || 0;; // get value of intime
        var outtime= $("#outtime").val() || 0;; // get value of outtime
        var day = 0; // declare day as 0
        if (intime == 08:00:00 && outtime >= 18:00:00) {
            day == 1;
        }else if (intime == 08:00:00 && outtime == 12:30:00) {
            day == 0.5;
        }else (intime == 12:30:00 && outtime >= 18:00:00) {
            day == 0.5;
        };
    });
});   

答案 1 :(得分:0)

获取intime和outtime的值。并检查代码中的语法错误有多个。

<script>
$(document).ready(function (){
    $('#intime').on('input', function() {
        var intime = $("#intime").val();
        var outtime= $("#outtime").val();
        if (intime == '08:00:00' && outtime >= '18:00:00') {
            day == 1;
        }else if (intime == '08:00:00' && outtime == '12:30:00') {
            day == 0.5;
        }else if(intime == '12:30:00' && outtime >= '18:00:00') {
            day == 0.5;
        }
    });

    $(document).ready(function (){
      $('#outtime').on('input', function() {
        var intime = $("#intime").val();
        var outtime= $("#outtime").val();
        if (intime == '08:00:00' && outtime >= '18:00:00') {
            day == 1;
        }else if (intime == '08:00:00' && outtime == '12:30:00') {
            day == 0.5;
        }else if(intime == '12:30:00' && outtime >= '18:00:00') {
            day == 0.5;
        }
      });
    });
});
</script>

答案 2 :(得分:0)

您可以使用moment.js来解析时间。

$(function(){
  $('#intime,#outtime').on('input', function() {
        // declare variables
        var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime
        var outtime= new moment($("#outtime").val(), 'HH:mm:ss'); 
        
        var startTime = new moment("08:00:00", 'HH:mm:ss');
        var halfDay = new moment("12:30:00", 'HH:mm:ss');
        var wholeDay = new moment("18:00:00", 'HH:mm:ss');
                
        
        var day = 0; // declare day as 0
         if (moment(intime).hour() == 8 && moment(outtime).hour() >= 18) {
            day = 1;
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 12 && moment(outtime).minute() == 30)) {
			day = 0.5;
		}
     else if ((moment(intime).hour() == 12 && moment(intime).minute() == 30) && moment(outtime).hour() >= moment(wholeDay).hour()) {
            day = 0.5;
        }
      $("#day").val(day);
      /*console.log("imtime", moment(intime).hour());
      console.log("intime", moment(intime).minute());
      console.log("outtime",moment(outtime).hour());
      console.log("outtime",moment(outtime).minute());
      console.log("wholeDay", moment(wholeDay).hour());*/
      
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<fieldset> In-Time:
   <input class="input" type="text" name="InTime" id="intime" value="" />
</fieldset>
<fieldset>Out-Time:
    <input class="input" type="text" name="OutTime" id="outtime" value="" />
</fieldset>
<fieldset> Day:
   <input class="input" type="text" id="day" name="Day" value="" />
</fieldset>