如何用keyup计算onchange

时间:2015-11-17 12:20:08

标签: jquery

我有3个输入。套餐 - 总价格 - 价格

如果更改包裹,我如何使价格也发生变化。

$('#the_total').keyup(function () {
    var the_total = $("#the_total").val();
    var get_package = $("#package").val();
    if (get_package == 'Package A') {
        var total = the_total * 100;
    } else {
        var total = the_total * 200;
    }
    $("#price").val(total);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="package">
    <option value="Package A">Package A</option>
    <option value="Package B">Package B</option>
</select>
<input type="text" id="the_total" name="total">
<input type="text" id="price" name="price">

5 个答案:

答案 0 :(得分:2)

触发更改事件 <android.support.v7.widget.SearchView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/searchView" android:background="#00000000" android:iconifiedByDefault="false" android:layout_weight="1" />

keyup
$('#the_total').keyup(function() {
  var the_total = $("#the_total").val();
  var get_package = $("#package").val();
  if (get_package == 'Package A') {
    var total = the_total * 100;
  } else {
    var total = the_total * 200;
  }
  $("#price").val(total);
});

$('#package').change(function() { // on change
  $('#the_total').keyup(); // trigger this
});

答案 1 :(得分:2)

试试这个:

$(document).ready(function(){
   $('#the_total').keyup(function() {
       loadPrice();
  });
  $('#package').change(function(){
      loadPrice();
  })
  function loadPrice() {
    var the_total = $("#the_total").val();
    var get_package = $("#package").val();
    if (get_package == 'Package A') {
      var total = the_total * 100;
    } else {
      var total = the_total * 200;
    }
    $("#price").val(total);
  }

});

答案 2 :(得分:2)

您可以尝试这样的事情:

$(function(){
  function doTotal(){
      var the_total = $("#the_total").val();
      var get_package = $("#package").val();
      var total = 0;
      get_package == 'Package A'?total = the_total * 100:total = the_total * 200;
      $("#price").val(total);
  }
  $('#the_total').keyup(function() {
    doTotal();
  });

  $('#package').change(function(){
    doTotal();
  });
});

工作小提琴:https://jsfiddle.net/tth4x2o9/1/

答案 3 :(得分:1)

使用keyupchangeon附加到这两个元素。

&#13;
&#13;
$('#the_total, #package').on('change keyup', function() {
  var the_total = $("#the_total").val();
  var get_package = $("#package").val();
  if (get_package == 'Package A') {
    var total = the_total * 100;
  } else {
    var total = the_total * 200;
  }
  $("#price").val(total);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="package">
  <option value="Package A">Package A</option>
  <option value="Package B">Package B</option>
</select>

<input type="text" id="the_total" name="total">
<input type="text" id="price" name="price">
&#13;
&#13;
&#13;

答案 4 :(得分:1)

使用此js - 验证很少

$(function() {

    function theChange () {
        var the_total = $("#the_total").val();
        var get_package = $("#package").val();

        if (get_package == 'Package A') {
            var total = the_total * 100;
        } else {
            var total = the_total * 200;
        }

        $("#price").val(total); 
    }

    $('#the_total').keyup(function() { theChange(); });
    $("#package").change(function () { theChange(); });
});