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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…