Javascript获取值并更改此值(重复值)

时间:2018-02-05 21:12:30

标签: javascript

我想将price_buy_id的值乘以valCurrecny

<select class="form-control" name="currency_price_buy" onchange="changePrice(this.options[this.selectedIndex].getAttribute('valCurrecny'));">
     <option value="1" valcurrecny="1.259200000000">AUD</option>
     <option value="2" valcurrecny="1.572200000000">BGN</option>
     <option value="3" valcurrecny="3.229400000000">BRL</option>
     <option value="4" valcurrecny="1.243400000000">CAD</option>
     <option value="33" valcurrecny="1.000000000000" selected="">USD</option>
     <option value="32" valcurrecny="12.029000000000">ZAR</option>
</select>

<input id="price_buy_id" value="7178.21">

和JS代码:

function changePrice(valCurrecny) {
  var getSelectOption = document.getElementById("price_buy_id").value;
  var multiplication = getSelectOption * valCurrecny;
  // console.log(multiplication);
  document.getElementById("price_buy_id").value = multiplication;
}

我遇到的问题我将currency_price_buy的值改变了几次,然后重复乘法结果。如何停止复制值

1 个答案:

答案 0 :(得分:1)

您需要以某种方式包含原始价格。例如,在price_buy_id中的数据属性 - 元素:

function changePrice(valCurrecny) {
  var getSelectOption = document.getElementById("price_buy_id").data('orig'); 
  var multiplication = getSelectOption * valCurrecny;
  // console.log(multiplication);
  document.getElementById("price_buy_id").value = multiplication;
}