如何在Django中的Select小部件中添加onchange js事件? [英] How can i add a onchange js event to Select widget in Django?
问题描述
我需要将JavaScript onchange事件附加到Django项目中的接收下拉列表。更改下拉列表的值时,将调用JavaScript函数。怎么做到呢? form.py 文件如下所示:
I need to attach a JavaScript onchange event to the receipt dropdown list in a Django project. When the value of the dropdown list is changed a JavaScript function is to be called. How can it be done? The form.py file is given below
from django import forms
receipt_types=(('option1','Option 1'),('option2','Option 2'),('option3','Option 3'),)
class accountsInForm(forms.Form):
receipt=forms.CharField(max_length=100, widget=forms.Select(choices=reciept_types))
推荐答案
按如下所示更改代码:
reciept = forms.ChoiceField(reciept_types,小部件= form.Select(attrs = {'onchange': myFunction();}))
Change the code as follows :
reciept=forms.ChoiceField(reciept_types, widget = forms.Select(attrs = {'onchange' : "myFunction();"}))
如果您希望访问JavaScript代码中的输入值,常见:
In case you want to have access to the input value in your JavaScript code, which is very common:
{'onchange' : "myFunction(this.value);"}
和JS:
myFunction(value) {
console.log(value)
}
这篇关于如何在Django中的Select小部件中添加onchange js事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!