Javascript 更改剑道ui下拉列表的选定值
我有一个想法:Javascript 更改剑道ui下拉列表的选定值,javascript,jquery,asp.net-mvc,kendo-ui,telerik,Javascript,Jquery,Asp.net Mvc,Kendo Ui,Telerik,我有一个想法: $("#Instrument").kendoDropDownList({ dataTextField: "symbol", dataValueField: "symbol", dataSource: data, index: 0 }); 如何使用jQuery更改它的选定值? 我试过: 但它并没有像预期的那样工作。您必须使用Kendo UI DropDownListselect方法(中的文档) 基本上你应该: // get a reference
$("#Instrument").kendoDropDownList({
dataTextField: "symbol",
dataValueField: "symbol",
dataSource: data,
index: 0
});
如何使用jQuery更改它的选定值?
我试过:
但它并没有像预期的那样工作。您必须使用Kendo UI DropDownList
select
方法(中的文档)
基本上你应该:
// get a reference to the dropdown list
var dropdownlist = $("#Instrument").data("kendoDropDownList");
如果您知道可以使用的索引:
// selects by index
dropdownlist.select(1);
如果没有,请使用:
// selects item if its text is equal to "test" using predicate function
dropdownlist.select(function(dataItem) {
return dataItem.symbol === "test";
});
jsIDLE示例由于这是与此相关的问题的顶级搜索结果之一,我觉得值得一提的是如何使用Kendo().DropDownListFor()实现此功能 除了根据文本和选择器选择项目外,所有内容都与OnaBai的帖子相同 为此,您需要将dataItem.symbol替换为dataItem。[DataTextFieldName]。无论您用于.DataTextField()的模型字段是什么,都将与之进行比较
@(Html.Kendo().DropDownListFor(model => model.Status.StatusId)
.Name("Status.StatusId")
.DataTextField("StatusName")
.DataValueField("StatusId")
.BindTo(...)
)
//So that your ViewModel gets bound properly on the post, naming is a bit
//different and as such you need to replace the periods with underscores
var ddl = $('#Status_StatusId').data('kendoDropDownList');
ddl.select(function(dataItem) {
return dataItem.StatusName === "Active";
});
可以通过值“本机”选择:
dropdownlist.select(1);
最简单的方法是:
$("#Instrument").data('kendoDropDownList').value("A value");
以下是最简单的方法。至少在剑道UI v2015.2.624中,似乎有一种更简单的方法:
$('#myDropDownSelector').data('kendoDropDownList').search('Text value to find');
如果下拉列表中没有匹配项,剑道似乎会将下拉列表设置为未选择的值,这是有意义的
我无法开始工作,但如果您将他的
值
与搜索
进行交换,如上所述,我们就成功了。按索引选择很好,但第二种解决方案不起作用。我在返回语句之前的函数中尝试了alert(dataItem.text),但它表示未定义。很抱歉,我已经编辑了答案,应该是dataItem.symbol
。参见JS Fiddle@anilca如果你不知道索引,你也可以通过文本选择(dataItem.Text)是否可以使用angularjs更新所选项目?如果你的KendoDropdown有一个change
事件,你必须通过调用dropdownlist.trigger(“change”)手动触发它
调用select
后,我做了一个示例,它成功了:我尝试了.search('A value')和.value('A value')这两种方法,但我没有看到任何差异。当我的ddl值中有逗号时,我无法实现这一点。例如,如果下拉列表中有“Horsesface,USA”,而我搜索“USA”时未找到任何结果。@redwards510我现在没有激活的剑道项目,但我建议您必须有一个与搜索
完全匹配的项目。我同意这个名字不好的函数。对于您的情况,您需要使用原始数据编写您自己的搜索例程,找到一个精确匹配[再次,通过您自己的搜索例程],然后选择via-search
-函数找到您找到的精确匹配。听起来不错?是的,不太明显的“搜索”很有效,谢谢。[为什么我会有索引,如本页前面所述。我没有实现Kendo,只是使用小部件…]我在我的项目@ÖmürBilgili中得到了“Uncaught TypeError:cannotread property'value'of undefined”error您可能需要首先检查未定义:var inst=$(“#Instrument”).data('kendoDropDownList');如果(inst){inst.value(“A值”);}
这与剑道的预期效果不符。它不会触发事件,因此不会更新显示。
$('#myDropDownSelector').data('kendoDropDownList').search('Text value to find');