我已经使用django-smart-select实现了相互依赖的dropdowns。默认情况下,下拉列表中的条目按字母顺序排序,这很好。但是,我需要有文本“其他”的条目,以显示在下拉列表的最后。
在django中有没有一种简单的方法来做这件事--智能选择?
发布于 2015-09-10 13:45:30
我最终只是用js写了一些快速的东西来解决这个问题。这段代码需要清理,但它做到了:)
<script>
$(document).ready(function(){
$('#id_subcategory').change(function(e){
var deletedOtherVal = 0
var select=document.getElementById('id_subcategory');
var selectedValue = $('#id_subcategory').find(":selected").text();
if (selectedValue != "Other"){
for (i=0;i<select.length; i++) {
if (select.options[i].text == "Other" ) {
deletedOtherVal = select.options[i].value
select.remove(i);
}
}
select.options[select.options.length] = new Option('Other', deletedOtherVal);
}
});
// And now fire change event when the DOM is ready
$('#id_subcategory').trigger('change');
});
</script>https://stackoverflow.com/questions/32405729
复制相似问题