onchange 函数不适用于十进制字段

时间:2021-01-12 16:53:35

标签: javascript django

我对 JS 不是很熟悉,我正在做 django 项目,当模型上的“price_buy”字段设置为 DecimalField 时,此功能不起作用,但如果是 FloatField,则它工作正常。此外,如果另一个字段是 None,我会收到此错误消息(未捕获的 ReferenceError:None is not defined at HTMLSelectElement.select.onchange ((index):552))。我该如何解决这个问题?

<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 模型:

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

0 个答案:

没有答案