添加文本框和下拉值(数学)

时间:2014-12-09 02:30:39

标签: javascript jquery html

我有这个下拉菜单,有三个值和两个文本框。我想要添加他们的价值观?我该怎么做?

这是我到目前为止所得到的......

HTML

<select id="as">
       <option value="10.00">Additional of $10.00</option>
       <option value="20.00">Additional of $10.00</option>
       <option value="30.00">Additional of $10.00</option>
</select>

 <input type="text" id="txt1"/> //the value here is automatically shown
 <input type="text" id="txt2"/> //the value here is automatically shown
 <input type="text" id="total"/> //this is where the answer shows

的Javascript

function subtotal(){
$("#as").change(function(){
    if ($(event.target)=='10.00'){

        var a = $("#txt1").val();
        var b = $("#txt2").val();
        var totalA = parseFloat(a, 10)+ parseFloat(b, 10);

        totalA = totalA + 10.00;
        $('#total').val('$' + totalA.toFixed(2));
    }else if ($(event.target)=='20.00'){
        var a = $("#txt1").val();
        var b = $("#txt2").val();
        var totalA = parseFloat(a, 10)+ parseFloat(b, 10);

        totalA = totalA + 20.00;
        $('#total').val('$' + totalA.toFixed(2));
    } else {
        var a = $("#txt1").val();
        var b = $("#txt2").val();
        var totalA = parseFloat(a, 10)+ parseFloat(b, 10);

        totalA = totalA + 30.00;
        $('#total').val('$' + totalA.toFixed(2));
    });
    }

P.S。你可以忽略文本框中值的自动显示,因为我已经完成了它并且没有错误。我主要担心的是添加下拉值和文本框值

2 个答案:

答案 0 :(得分:0)

DEMO从这开始..希望它会有所帮助

subtotal();
function subtotal(){
$("#as").on('change',function(event){
    if ($(this).val() =='10.00'){

        var a = $("#sff").val();
        var b = $("#osf").val();
        var totalA = parseFloat(a) + parseFloat(b);

        totalA = parseFloat(totalA) + 10.00;
        $('#total').val('$' + totalA);
    }else if ($(this).val() =='20.00'){

        var a = $("#sff").val();
        var b = $("#osf").val();
       var totalA = parseFloat(a) + parseFloat(b);

        totalA = parseFloat(totalA) + 20.00;
        $('#total').val('$' + totalA);
    }else if ($(this).val() =='30.00'){

        var a = $("#sff").val();
        var b = $("#osf").val();
        var totalA = parseFloat(a) + parseFloat(b);

        totalA = parseFloat(totalA) + 30.00;
        $('#total').val('$' + totalA);
    }
    });
  }

答案 1 :(得分:0)

以下是添加select值的2个文本框的简化版本。

  <select id="as">
    <option value="10.00">Additional of $10.00</option>
    <option value="20.00">Additional of $20.00</option>
    <option value="30.00">Additional of $30.00</option>
  </select>

  <input type="text" id="txt1" />
  <input type="text" id="txt2" />
  <input type="text" id="total" />

  <script>
    $("#as").on('change', function(e) {
      var ddl = $(this).val();
      var txt1 = $('#txt1').val();
      var txt2 = $('#txt2').val();

      var a = parseFloat(ddl);
      var b = parseFloat(txt1);
      var c = parseFloat(txt2);
      var total = a + b + c;

      $('#total').val(total);
    });
  </script>

演示:http://plnkr.co/edit/JlAffVadhKiW6HnbWqNx?p=preview