您不需要使用 jQuery 从下拉列表中删除特定项目。
您可以通过使用 Kendo DataSource 对象及其 MVVM 模式来实现您想要的。
您的 HTML 将如下所示:
<input id='dropdownlist' data-role="dropdownlist"
data-text-field="ProductName"
data-value-field="ProductID"
data-bind="value: selectedProduct,
source: products" />
<button id="button" type="button">Remove current item</button>
<br />
<input class='k-textbox' id='specificItem' />
<button id="removeSpecificButton" type="button">Remove specific item</button>
你的 JavaScript 将是:
// Use a viewModel, so that any changes to the model are instantly applied to the view.
// In this case the view is the dropdownlist.
var viewModel = kendo.observable({
selectedProduct: null,
products: new kendo.data.DataSource({
transport: {
read: {
url: "//demos.telerik.com/kendo-ui/service/products",
dataType: "jsonp"
}
}
})
});
kendo.bind($("#dropdownlist"), viewModel);
$("#removeSpecificButton").kendoButton({
click: function(e) {
// Find the item specified in the text box
viewModel.products.filter( {
field: "ProductName",
operator: "eq",
value: $('#specificItem').val() });
// Apply the view to find it
var view = viewModel.products.view();
//remove the item from the datasource
viewModel.products.remove(view[0]);
// disable the filter
viewModel.products.filter({});
}
});
// Set-up the button so that when it is clicked
// it determines what the currently selected dropdown item is
// and then deletes it.
$("#button").kendoButton({
click: function(e) {
// Determine which item was clicked
var dropdownlist = $("#dropdownlist").data("kendoDropDownList");
var dataItem = dropdownlist.dataItem();
// Remove that item from the datasource
viewModel.products.remove(dataItem);
}
});
我在这里写了一个这样的例子:
http://dojo.telerik.com/@BenSmith/aCEXI/11
请注意 DataSource 的“过滤器”方法是如何用于指定要删除的确切项目(在本例中为 ProductName)的。删除项目后,可以删除过滤器以显示不再需要的项目的数据。
我还提供了一个工具,可以删除当前选定的项目以保持完整性。