我正在使用 Kendo UI 控件KendoUI Autocomplete和KendoUI MultiSelect。我需要在客户端以编程方式更改这些控件值及其显示,而无需用户对这些控件进行操作。
如何重现该行为:
- 请看这个JS fiddle链接,有两个 Autocomplete 控件。
- 请在每个控件中键入几个字符,然后从自动建议中选择一个结果。
- 然后单击 Reverse 按钮,JS 尝试更改(反向)值,但这不会完成,无需用户单击自动建议中填充的结果。
如何避免用户选择结果?如果用户单击 Reveres,则两个控件都应该有新的选定值,并且它的显示也应该反映它。如果我单击显示选择,那应该显示新的选定值吗?我对 MultiSelect 也有类似的问题,如果相信如果我解决了这个问题,同样的解决方案也适用于 MultiSelect。
HTML
<div class="demo-section k-content">
<h4>Find a product</h4>
<input id="From" style="width: 100%;" placeholder="From" />
<div class="demo-hint">Hint: type "che"</div>
<br />
<input id="To" style="width: 100%;" placeholder="To" />
<div class="demo-hint">Hint: type "unc"</div>
<br />
<p>Please select From and To then clcik on reverse</p>
<button id="reverse">
Reverse
</button>
<button id="show">
Show Selection
</button>
<p id="result">
</p>
</div>
JS
$(document).ready(function() {
$("#result").html("");
$("#From").kendoAutoComplete({
dataTextField: "ProductName",
filter: "contains",
minLength: 2,
dataSource: {
type: "odata",
serverFiltering: true,
transport: {
read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
}
}
});
$("#To").kendoAutoComplete({
dataTextField: "ProductName",
filter: "contains",
minLength: 2,
dataSource: {
type: "odata",
serverFiltering: true,
transport: {
read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
}
}
});
$("#reverse").click(function(e) {
$("#result").html("");
var $departure = $("#From").data("kendoAutoComplete");
var $destination = $("#To").data("kendoAutoComplete");
var departureStation = $departure.dataItem();
var destinationStation = $destination.dataItem();
$departure.search(destinationStation.icao_id);
$departure.select($departure.ul.children().eq(0));
$departure.value(destinationStation.DisplayName);
$departure.trigger("change");
$departure.close();
$destination.search(departureStation.icao_id);
$destination.select($destination.ul.children().eq(0));
$destination.value(departureStation.DisplayName);
$destination.trigger("change");
$destination.close();
});
$("#show").click(function(e) {
$("#result").html("");
var $departure = $("#From").data("kendoAutoComplete");
var $destination = $("#To").data("kendoAutoComplete");
var departureStation = $departure.dataItem();
var destinationStation = $destination.dataItem();
var from = JSON.stringify(departureStation);
var to = JSON.stringify(destinationStation);
$("#result").html("From :" + from + "<br/><br/><br/><br/>To: " + to);
});
});