默认选择kendo组合框(Default select kendo combobox)

编程入门 行业动态 更新时间:2024-10-16 18:39:50
默认选择kendo组合框(Default select kendo combobox)

我使用KendoUI ComboBox,我想放置一个默认选择项目。

在KendoUI组合框中,我没有找到将文本中的默认值和索引放在一起的方法。

<script> $("#combobox").kendoComboBox({ dataSource: [ { id: 1, name: "Apples" }, { id: 2, name: "Oranges" } ], dataTextField: "name", dataValueField: "id" }); var combobox = $("#combobox").data("kendoComboBox"); combobox.select(combobox.ul.children().eq(0)); </script>

这里是例子。 如何将其转换为放置文本?

I use KendoUI ComboBox and I want to put a default select item.

In KendoUI ComboBox I didn't find the way to put the default value in text and not with index.

<script> $("#combobox").kendoComboBox({ dataSource: [ { id: 1, name: "Apples" }, { id: 2, name: "Oranges" } ], dataTextField: "name", dataValueField: "id" }); var combobox = $("#combobox").data("kendoComboBox"); combobox.select(combobox.ul.children().eq(0)); </script>

Here is the example. How can convert it to put text?

最满意答案

正如@SatyaRanjanSahoo所说你应该使用value 你应该使用id值,否则你将强制一个可能不在DataSource中的值。

例如,如果你这样做:

var combobox = $("#combobox").data("kendoComboBox"); // Set Value combobox.value("Apricot"); // Get Value alert("Value is " + combobox.value());

这将显示Apricot但这不在DataSource ,如果你这样做:

var combobox = $("#combobox").data("kendoComboBox"); // Set Value combobox.value(2); // Get Value alert("Value is " + combobox.value());

这将显示Oranges ,这是id为2的项目的正确值。

因此,除非您确定value call中设置的value是有效的dataTextField我会推荐使用dataValueField 。

在下面的代码片段中检查这个...

$("#combobox").kendoComboBox({
  dataSource: [
    { id: 1, name: "Apples" },
    { id: 2, name: "Oranges" }
  ],
  dataTextField: "name",
  dataValueField: "id"
});

var combobox = $("#combobox").data("kendoComboBox");


// Set a valid value
combobox.value("Oranges");
alert("Value for Oranges is: " + combobox.value());

// Set an invalid value
combobox.value("Apricots");
alert("Value for Apricots is: " + combobox.value()); 
  
<link href="http://cdn.kendostatic.com/2014.2.1008/styles/kendo.common.min.css" rel="stylesheet" />
<link href="http://cdn.kendostatic.com/2014.2.1008/styles/kendo.default.min.css" rel="stylesheet" />
<script src="http://cdn.kendostatic.com/2014.2.1008/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.2.1008/js/kendo.all.min.js"></script>

<input id="combobox"/> 
  
 

As @SatyaRanjanSahoo says you should use value but you should use the id value otherwise you will be forcing a value that might not be in the DataSource.

Example, If you do:

var combobox = $("#combobox").data("kendoComboBox"); // Set Value combobox.value("Apricot"); // Get Value alert("Value is " + combobox.value());

this will show Apricot but this is not in the DataSource meanwhile, if you do:

var combobox = $("#combobox").data("kendoComboBox"); // Set Value combobox.value(2); // Get Value alert("Value is " + combobox.value());

This will show Oranges that is the correct value for the item which id is 2.

So, unless you are sure that the value set in value call is a valid dataTextField I would recommend using the dataValueField.

Check this in the following code snippet...

$("#combobox").kendoComboBox({
  dataSource: [
    { id: 1, name: "Apples" },
    { id: 2, name: "Oranges" }
  ],
  dataTextField: "name",
  dataValueField: "id"
});

var combobox = $("#combobox").data("kendoComboBox");


// Set a valid value
combobox.value("Oranges");
alert("Value for Oranges is: " + combobox.value());

// Set an invalid value
combobox.value("Apricots");
alert("Value for Apricots is: " + combobox.value()); 
  
<link href="http://cdn.kendostatic.com/2014.2.1008/styles/kendo.common.min.css" rel="stylesheet" />
<link href="http://cdn.kendostatic.com/2014.2.1008/styles/kendo.default.min.css" rel="stylesheet" />
<script src="http://cdn.kendostatic.com/2014.2.1008/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.2.1008/js/kendo.all.min.js"></script>

<input id="combobox"/> 
  
 

更多推荐

KendoUI,ComboBox,文本,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-08-01 14:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   kendo   Default   combobox   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!