用于下拉选定索引更改的java脚本

时间:2009-09-17 10:01:07

标签: javascript jquery

i 2下拉控件和1个文本框  在下拉列表1中,我的值为11.00,11.30,12.00,12.30,13.00,13.30  在下拉2中我有像11.30,12.00,12.30,13.00,13.30,14.00这样的值

一旦用户选择下拉1中的值并下拉2,事件将在下拉2中触发。其中从下拉1和下拉2中选择的值将被采用。 dropdown1值从dropdown2值中减去  如果条件如11.30 - 11.00 = .30如果.30是结果,它应该在文本框中显示为.50  如果条件如11.00 - 10.30 = .70如果.70是结果,它应该在文本框中显示为.50

这个条件应该在javascript中工作

如果有人知道该怎么做,请帮帮我   谢谢

3 个答案:

答案 0 :(得分:1)

在你的下拉列表中添加'onchange =“calculate()”' 添加此javascript函数

function calculate()
{
   var dropdown1 = document.getElementById('dropdown1')
   var dropdown2 = document.getElementById('dropdown1')
   var textbox = document.getElementById('textbox')
   var a = dropdown1.options[dropdown1.selectedIndex]
   var b = dropdown2.options[dropdown2.selectedIndex]
   if((a -b) == what ever you want)
      textbox.value = what ever you want
   else if((a -b) == what ever you want)
      textbox.value = what ever you want

}

答案 1 :(得分:0)

jQuery中没有替换部分的代码。我不确定你的两个条件。如果结果是.30或.70。

,你想要总是显示.50吗?

<强>更新

<script>
$(document).ready ( function () {      
  $("#sel1").change ( function () {      
    CalcResult ();
  });

  $("#sel2").change ( function () {    
     CalcResult ();
  });


  function CalcResult ()
  {
    var resultVal = parseFloat ( $("#sel2 option:selected").val() ) - parseFloat ( "#sel1 option:selected").val() );         

    var resultVal = $("#sel2 option:selected").val() - $("#sel1 option:selected").val();

    resultVal = resultVal.toFixed("2").toString().replace ( ".30" , ".50" ).replace(".70" , ".50" );

    $("#txt1").val(resultVal.toFixed("2"));
}

});
</script>

<body>
<select id="sel1"> 
    <option value="11.00">11.00</option> 
    <option value="11.30">11.30</option> 
    <option value="12.00">12.00</option> 
    <option value="12.30">12.30</option> 
    <option value="13.00">13.00</option> 
    <option value="13.3">13.30</option> 
  </select> 
  <select id="sel2"> 
    <option value="11.30">11.30</option> 
    <option value="12.00">12.00</option> 
    <option value="12.30">12.30</option> 
    <option value="13.00">13.00</option> 
    <option value="13.30">13.30</option> 
    <option value="14.00">14.0</option> 
  </select> 

  <input type="text" id="txt1" /> 
</body> 

答案 2 :(得分:0)

我有点心血来潮,我决定你想要计算两次之间的差异...如果不是你想要的话,请忽略我,但无论如何我还是喜欢这样做。为了方便起见,我将下拉列表中的值设置为分钟数,因为在js中使用时间可以是一个球疼:

<script type="text/javascript">
    $(document).ready(function() {      
        //Calculate the difference when second ddl changes
        $("#ddl2").change(function() {calculateTimeDiff();});
   });

   function calculateTimeDiff() {
    //get the values
    var time1 = $("#ddl1").val();   
    var time2 = $("#ddl2").val();

    //check everything is in the right state to do the calc
    if(time1 > time2) {
        alert("time 1 must be before time 2");
    }
    else {              
        //get the number of mins difference
        var minsDiff = (time2 - time1);     
        //remove number of hours (60 min chunks)
        minsDiff = minsDiff % 60;
        //get hours difference
        var hoursDiff = time2 - time1 - minsDiff;
        //if more than none, get the no of whole hours
        if (hoursDiff > 0) {        
            hoursDiff = hoursDiff/60;           
        }
        //format mins diff
        if(minsDiff == 0) { minsDiff = "00"};
        //add it to text box
        $("#text1").val(hoursDiff + ":" + minsDiff);                                
    }   
   }
</script>

<div id="example">
    <select id="ddl1"> 
        <option value="600">10:00</option>
        <option value="630">10:30</option>
        <option value="660">11:00</option>
        <option value="690">11:30</option>
        <option value="720">12:00</option>
    </select>

    <select id="ddl2"> 
        <option value="630">10:30</option>
        <option value="660">11:00</option>
        <option value="690">11:30</option>
        <option value="720">12:00</option>
        <option value="750">12:30</option>
    </select>

    <input type="text" id="text1" />
</div>