如何在javascript中更改选择的值

时间:2016-10-03 10:56:11

标签: javascript html

这是我的代码,工作正常。但我想在休假类型选择中添加它 当选择离开类型然后它显示计数,当leavetype为shortleave时,count应该是0.25,当半天然后是0.50。否则正常工作任何身体都可以帮助我这方面..谢谢提前

<form>    

    Leave Type:
    <select id="filter" name="leavetype" required>
                  <option value="selected">Select Leave Type</option>
                  <option value="Absent" >Absent</option>
                  <option value="Annual" >Annual</option>
                  <option value="Medical" >Medical</option>
                  <option value="Casual" >Casual</option>
                  <option value="LWOP" >LWOP</option>
                  <option value="Short Leave" >Short Leave</option>
                  <option value="Half Day" >Half Day</option>
                  </select>
    <br> Leave From Date
    <input type="text" id="leavefromdate">
    <br> Leave To Date
    <input type="text" id="leavetodate">
    <br> Leave Count
    <input type="text" id="leavecount">
    <br>   

</form>    

<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css" />
<script>
    $(document).ready(function() {

        $("#leavefromdate,#leavetodate").datepicker({
            changeMonth: true,
            changeYear: true,
            firstDay: 1,
            dateFormat: 'dd/mm/yy',
        })

        $("#leavefromdate").datepicker({
            dateFormat: 'dd-mm-yy'
        });
        $("#leavetodate").datepicker({
            dateFormat: 'dd-mm-yy'
        });

        $('#leavetodate').change(function() {
            var start = $('#leavefromdate').datepicker('getDate');
            var end = $('#leavetodate').datepicker('getDate');

            if (start <= end) {
                var leavecount = (end - start) / 1000 / 60 / 60 / 24 + 1;
                $('#leavecount').val(leavecount);
            }
            else {
                alert("You cant come back before you have been!");
                $('#leavefromdate').val("");
                $('#leavetodate').val("");
                $('#leavecount').val("");
            }
        }); //end change function
    }); //end ready
</script>

4 个答案:

答案 0 :(得分:0)

按照以下步骤来达到您的要求。

  1. 在休假类型的Onchange事件中选择:对于“短假”请假 计数0.25和“半假”使得假计数“0.50”。

  2. 选择休假类型“短假”和“半休假” “保留日期”字段禁用。

  3. 对于任何其他休假类型选择,请启用“从日期保留”字段,计算将保持不变。

  4. 对于任何其他leavetype,请启用“Leave From Date”字段并按原样进行计算。

    请查看以下工作片段。

    $(document).ready(function() {
      $("#filter").on("change",function(){
        var leaveType = $(this).val();
        if(leaveType=='Short Leave'){
          $('#leavecount').val(0.25);
          $('#leavetodate').prop("disabled",true);
          $('#leavetodate').val("");
        }else if(leaveType=="Half Day"){
          $('#leavecount').val(0.25);      
          $('#leavetodate').prop("disabled",true);
          $('#leavetodate').val("");
        }else{
          $('#leavetodate').prop("disabled",false);
        }
      });
      $( "#leavefromdate,#leavetodate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        firstDay: 1,
        dateFormat: 'dd/mm/yy',
      })
    
      $( "#leavefromdate" ).datepicker({ dateFormat: 'dd-mm-yy' });
      $( "#leavetodate" ).datepicker({ dateFormat: 'dd-mm-yy' });
    
      $('#leavetodate').change(function() {
        var start = $('#leavefromdate').datepicker('getDate');
        var end   = $('#leavetodate').datepicker('getDate');
    
        if (start<=end) {
          var leavecount   = (end - start)/1000/60/60/24+1;
          $('#leavecount').val(leavecount);
        }
        else {
          alert ("You cant come back before you have been!");
          $('#leavefromdate').val("");
          $('#leavetodate').val("");
          $('#leavecount').val("");
        }  
      }); //end change function
    }); 
    <script src="https://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="https://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css" />
    <form>    
    
      Leave Type:
      <select id="filter" name="leavetype" required>
        <option value="selected">Select Leave Type</option>
        <option value="Absent" >Absent</option>
        <option value="Annual" >Annual</option>
        <option value="Medical" >Medical</option>
        <option value="Casual" >Casual</option>
        <option value="LWOP" >LWOP</option>
        <option value="Short Leave" >Short Leave</option>
        <option value="Half Day" >Half Day</option>
      </select>
      <br> Leave From Date
      <input type="text" id="leavefromdate">
      <br> Leave To Date
      <input type="text" id="leavetodate">
      <br> Leave Count
      <input type="text" id="leavecount">
      <br>  
    </form>    

答案 1 :(得分:0)

您可以相应地设置值。

<select id="filter" name="leavetype" required>
     <option value="selected">Select Leave Type</option>
     <option value="Absent" >Absent</option>
     <option value="Annual" >Annual</option>
     <option value="Medical" >Medical</option>
     <option value="Casual" >Casual</option>
     <option value="LWOP" >LWOP</option>
     <option value="0.25" >Short Leave</option>
     <option value="0.5" >Half Day</option>
</select>

并且从下拉列表中获取值时,现在您将计数值作为值。

答案 2 :(得分:0)

这是一个工作示例。只需为您的选项添加值,或检查字符串,如下所示:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] 
public void GetList()
{
      JavaScriptSerializer ser = new JavaScriptSerializer();

      Test[] test=new Test[3];
      test[0] = new Test("1", "anex");
      test[1] = new Test("2", "Ahmed");
      test[2] = new Test("3", "Karim");

      var json = new
      {
          list=test ,
          DateTime=DateTime.Now.ToShortDateString()                             
      };

      HttpContext.Current.Response.Write(ser.Serialize(json)); 
}

{"list":[{},{},{}],"DateTime":"03/10/2016"}
var leavefactor = 1;
      if ($("select#filter").val() == 'ShortLeave') {leavefactor = 0.25;} else if($("select#filter").val() == 'HalfDay') {leavefactor = 0.5;}
      $('#leavecount').val(leavecount * leavefactor);

答案 3 :(得分:0)

您可以为您的下拉过滤器和onChange事件添加更改事件,您可以触发并更改离开计数值,如下所示。

$('#filter').on('change', function (e) {

    var valueSelected = this.value;
    var value ;
   switch(valueSelected ) {
    case "Half Day":
        vaule =  $('#leavecount")val();
        value =  value/2;
        $('#leavecount")val(value);
        break;
    case "Short Leave":
        vaule =  $('#leavecount")val();
        value =  value/4;
        $('#leavecount")val(value);
        break;
    default:
        default ;
}


});