2014-12-01 74 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")