我有2个使用autocomplete-light的自动填充下拉列表 我希望第二个dropdownlist(汽车)的可见性取决于第一个dropdownlist(raste)的值,但我不知道如何找到他们的ID?
Forms.py:
class StocksForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(StocksForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
field.widget.attrs['style']= 'width:60%'
class Meta():
model=Stocks
fields=('user','raste','car','stname','mark','description','pic','price')
widgets = {
'raste': autocomplete.Select2(url='BallbearingSite:stock_autocomplete'),
'car': autocomplete.Select2(url='BallbearingSite:car_autocomplete'),
}
HTML:
<form enctype="multipart/form-data" method="post" >
{% csrf_token %}
{{ stocks_form.as_p }}
<input id="savestocks" type="submit" name="" value="ثبت">
<input type="text" name="search" id="txtSearch" onkeyup="searchOpen()" />
<input type="text" id="id_input">
<div>
<input type="text" id="contact_name_search_input" name="contact_name_search" />
</div>
<div class="ui-widget">
<label for="places">Places: </label>
<input id="places">
</div>
</form>
这些是我写的javascript代码,但是不起作用!
<script>
$(document).ready(function() {
$('#select2-id_raste-container').change(function() {
var selectedValue = $(this).val();
if(selectedValue === 'abc') {
$('#select2-id_car-container').closest('span').hide();
} else if (selectedValue === 'bcd') {
$('#select2-id_car-container').closest('span').show();
}
});
});
</script>
firefox领事中的html代码:
<label for="id_raste">Raste:</label>
<select class="form-control select2-hidden-accessible" data-autocomplete-light-function="select2" data-autocomplete-light-url="/stock_autocomplete/" id="id_raste" name="raste" style="width:60%" tabindex="-1" aria-hidden="true">
</select>
<span class="select2 select2-container select2-container--default" dir="ltr" style="width: 60%;">
<span class="selection">
<span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-id_raste-container">
<span class="select2-selection__rendered" id="select2-id_raste-container"><span class="select2-selection__placeholder">
</span>
</span>
<span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span></span>