Bas*_*jar 2 jquery select optgroup
我的问题很简单,但我没有找到这样做的明确例子.我正在尝试为选择添加optgroup并使用jquery填充选项,因为某些原因它不起作用.这是我的代码:
$('#ptype').change(function() {
$.post(
"/sell/site/index/categories",
{
'parent_id': $('#ptype').val()
},
function(response){
var categories = $.parseJSON(response);
$('#sub_category').empty();
$.each(categories, function (index) {
var optgroup = $('<optgroup/>');
$.each(this.children, function (index) {
optgroup.add($("<option></option>")
.attr("value",index)
.text(this));
});
$("#sub_category").append(optgroup);
});
$("#sub_category").multiselect('refresh'); //refresh the select here
}
);
});
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!谢谢!
Bas*_*jar 17
好的,所以我最终想出了解决方案,但是,感谢你们的有用提示.这是工作代码:
$('#ptype').change(
function() {
$.ajax({
type: 'POST',
url: "/sell/site/index/categories",
data: { 'parent_id': $('#ptype').val() },
success: function(data){ updateCategories(data); },
dataType: 'json'
})
}
);
function updateCategories(categories){
$('#sub_category').empty();
$.each(categories, function (index) {
var optgroup = $('<optgroup>');
optgroup.attr('label',categories[index].name);
$.each(categories[index].children, function (i) {
var option = $("<option></option>");
option.val(i);
option.text(categories[index].children[i]);
optgroup.append(option);
});
$("#sub_category").append(optgroup);
});
$("#sub_category").multiselect('refresh'); //refresh the select here
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23131 次 |
最近记录: |