Welcome to ZaiNaLe Developer Community-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - onchange function doesn't work with decimal field

I'm not very familiar with JS, i m working on django project, when a 'price_buy' field is set to DecimalField on models, this function doesn't work, but if FloatField, it works fine. Also if another field is None, i get this error msg (Uncaught ReferenceError: None is not defined at HTMLSelectElement.select.onchange ((index):552)). How i can resolve this problem ?

<script type="text/javascript">
  document.querySelectorAll("select").forEach((select) => {
    select.onchange = function(){
      var price_buy = 0;
      const value = this.value;
      {% autoescape off %}
      const products = {{ products }}
      {% endautoescape %}
      for (var index=0; index < products.length; index++ ) {
        if (products[index].id == value) {
            price_buy = products[index].price_buy;
        }
      }
      const name = this.name.replace("product", "price_buy")
      console.log(price_buy, name);
      document.querySelector(`input[name=${name}]`).value = price_buy;
    }
  });
</script>

Django Model:

class Product(models.Model):
    name = models.CharField(max_length=64)
    price_buy = models.DecimalField()
    production = models.FloatField(blank=True, null=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to ZaiNaLe Developer Community-Open, Learning and Share
...