我的问题很简单,但我没有找到一个明确的例子。 我想为选择添加optgroup,并用jquery填充选项,因为它不起作用。 这是我的代码:添加选项组与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
}
);
});
任何帮助将不胜感激! 谢谢!的
$.each(this.children, function (index) {
this
你不应该需要调用'$ .parseJSON()' - 设置''json''一个'dataType'和jQuery会自动解析它,并将结果传递到您的回电话。如果你没有明确地设置数据类型,jQuery会根据响应进行智能猜测,所以它可能仍然会尝试为你解析它,在这种情况下,你尝试再次使用'$ .parseJSON()'解析它将不会工作。 – nnnnnn