Hi I'm having issue with passing data to jquery. In my select I have added onchange method and it calls getProductData and pass this select element. In getProductData I get the element and display the selected value. I tried using few methods but the output of alert is [object Object] or undefined.
I'm not using $('#select_id').on('change') because it in a dynamic row and each id for tr, select and input are the same. For a better understanding, please view this image
forms.py
PD_name = forms.ModelChoiceField(
label = "Product Name",
queryset = Product.objects.only('name').order_by('id'),
to_field_name = 'id',
empty_label = "",
widget = forms.Select(
attrs = {
'class': 'form-control',
'onblur': """form_validation({
'element' : this,
})""",
'onchange': """getProductData({
'element' : this,
})"""
}
)
)
html
<td>{{ Productform.PD_name }}</td>
JQuery
function getProductData(element) {
alert(element.value);
}
CodePudding user response:
Forms:
{'onchange' : "getProductData(this.value);"}
JS:
getProductData(value) {
console.log(value)
}