2014-12-01 48 views
-1

我有下面的代码。在弹出的jQuery对话框中有两个下拉列表。我使用jQuery来填充它们。问题是,当我单击编辑图标(在每个产品项目右侧)时,它可以在弹出的下拉列表中显示所选项目,但有时不会。使用萤火虫检查代码,它显示jQuery已经改变了代码,但是屏幕没有显示选择的项目。jQuery不刷新jQuery对话框上的下拉列表

function getProductById(ProductId) { 
    $.getJSON("GetProductById", { ProductId: ProductId }, function populateProduct(data) { 
     //Populate query result retreved from server into a Sub form for edit 
     $("#ProductName").val(data.ProductName); 
     $("#UnitPrice").val(data.UnitPrice); 
     $("#Discontinued").attr("checked", data.Discontinued); 

     $("#Supplier option:selected").removeAttr("selected"); 
     $("#Supplier option[value=" + "\"" + data.Supplier + "\"" + "]").attr("selected", "selected"); 

     $("#Categorie option:selected").removeAttr("selected"); 
     $("#Categorie option[value=" + "\"" + data.Category + "\"" + "]").attr("selected", "selected"); 
    }); 
}; 

enter image description here

enter image description here

enter image description here

+0

可能是一些事情。首先,尝试使用'.prop('selected','selected')'而不是'.attr(“selected”,“selected”)' – Victor 2014-12-01 20:51:02

+0

@Victor。非常感谢你的帮助!它现在有效!那么为什么我的代码不工作,但在改变为.prop('selected','selected')后工作? – 2014-12-01 20:58:51

+0

这是没有必要的。如果data.Supplier包含的值与供应商下拉菜单中的一个选项值相匹配,那么您只需要使用$(“#Supplier').val(data.Supplier);'和同上'Category' – 2014-12-01 21:02:18

回答

1

如果您正在使用jQuery 1.9+与prop取代attr这是唯一没有实现向后兼容,并可能会在一个被删除不久的将来:.attr vs .prop。所以,而不是.attr("checked", data.Discontinued);尝试使用.prop("checked", data.Discontinued);并用.prop("selected", "selected")替换.attr("selected", "selected")