将所有CSS类添加到数组中并进行计算

时间:2016-10-03 17:55:51

标签: jquery html css

我需要在文本字段中放置汇率并更新多个文本区域费率需要更新

Exchange Rate <input type="text" value="" id="exchange_rate">
<input type="button" value="Update Price" id="update_price">

文本区域计数是动态的,这里有一些示例

<input class="price" name="pro_name[88197][1]" rel="4450000" value=""  type="text">
<input class="price" name="pro_name[12316][2]"  rel="4451000" value=""  type="text">
<input class="price" name="pro_name[46511][3]" rel="4575120" value=""  type="text">
<input class="price" name="pro_name[45151][4]" rel="2343400" value=""  type="text">
<input class="price" name="pro_name[165652][5]" rel="4534500" value=""  type="text">
<input class="price" name="pro_name[85559][6]" rel="4450000" value=""  type="text">

现在我需要将每个文本区域值都带到上面 rel * exchange_rate = value (rel是日本的YEN金额,汇率是USD)

我这样开始我的jquery并且卡住了,请帮帮我一个人!!

$("#update_price").click(function () {
   var all_price_element = [];
   price_class = $( ".price" ).toArray();

   for ( var i = 0; i < price_class.length; i++ ) {
     all_price_element.push( price_class[ i ].innerHTML );
   }

});

2 个答案:

答案 0 :(得分:1)

您可以更新以下所有价格。

$("#update_price").click(function () {  
    var ex_rate = $('#exchange_rate').val();   
  
    $(".price").each(function() {
        var rel = $(this).attr('rel')
        $(this).val(ex_rate * rel);
    });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="price" name="pro_name[88197][1]" rel="4450000" value=""  type="text">
<input class="price" name="pro_name[12316][2]"  rel="4451000" value=""  type="text">
<input class="price" name="pro_name[46511][3]" rel="4575120" value=""  type="text">
<input class="price" name="pro_name[45151][4]" rel="2343400" value=""  type="text">
<input class="price" name="pro_name[165652][5]" rel="4534500" value=""  type="text">
<input class="price" name="pro_name[85559][6]" rel="4450000" value=""  type="text">

Exchange Rate
<input type="text" value="" id="exchange_rate">
<input type="button" value="Update Price" id="update_price">

答案 1 :(得分:1)

您可以使用.each()

所以代码片段是:

$("#update_price").click(function (e) {
  //
  // get the current exchange_rate and convert it to number
  //
  var er = +document.getElementById('exchange_rate').value;

  
  //
  // for each price element do the calculation:
  //
  // get the current rel value, convert it to number and
  // multiply per rel
  //
  $( ".price").each(function(index, element) {
    element.value = er * +element.getAttribute('rel');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  Exchange Rate <input type="text" value="" id="exchange_rate">
<input type="button" value="Update Price" id="update_price">

<input class="price" name="pro_name[88197][1]" rel="4450000" value=""  type="text">
<input class="price" name="pro_name[12316][2]"  rel="4451000" value=""  type="text">
<input class="price" name="pro_name[46511][3]" rel="4575120" value=""  type="text">
<input class="price" name="pro_name[45151][4]" rel="2343400" value=""  type="text">
<input class="price" name="pro_name[165652][5]" rel="4534500" value=""  type="text">
<input class="price" name="pro_name[85559][6]" rel="4450000" value=""  type="text">
</form>