Javascript 选择项目时会触发两次kendoListView更改事件
问题: 当我通过代码选择listview中的第一个元素来触发change事件时,该事件只触发一次。 当我通过单击listview项触发更改事件时,更改事件会被触发两次 知道什么会触发第二次事件吗?如何预防 HTML代码:Javascript 选择项目时会触发两次kendoListView更改事件,javascript,listview,select,kendo-ui,onchange,Javascript,Listview,Select,Kendo Ui,Onchange,问题: 当我通过代码选择listview中的第一个元素来触发change事件时,该事件只触发一次。 当我通过单击listview项触发更改事件时,更改事件会被触发两次 知道什么会触发第二次事件吗?如何预防 HTML代码: <div id="overview"> <div id="listView"></div> </div> <!-- Used lib source loaded from lib server, for devel
<div id="overview">
<div id="listView"></div>
</div>
<!-- Used lib source loaded from lib server, for developers -->
<link rel="stylesheet" type="text/css" href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.common.min.css" />
<link rel="stylesheet" type="text/css" href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.default.min.css" />
<script src="http://cdn.kendostatic.com/2013.2.716/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2013.2.716/js/kendo.all.min.js"></script>
<!-- Custom libs loaded from meter, for customers/developers (very fast <0.3sec) -->
<script type="text/javascript" src="/MeasCon/Scripts/current_values.js"></script>
/* JQuery function that is called when the DOM is done loading */
$(document).ready(function ()
{
/* The HTML template code for the current values overview listview */
/* .join is for multi line in javascript */
var overview_template = [
'<div class="overview_content" id="#:name#" >',
'<div class="overview_content_textbox">',
'<span class="overview_content_textbox_text">#:test#</span>',
'</div>',
'<div class="overview_content_led_box">',
'<span class="led_box"><img class="led" src="/MeasCon/Content/IMAGES/#= led #.png"></span>',
'</div>',
'</div>'
].join("\n");
/* the datasource for the overview listview */
var overview_datasource = new kendo.data.DataSource(
{
transport:
{
read:
{
url: "../../Content/current_values_overview.json",
dataType: "json"
}
},
});
/* populating the listview with the datasource as defined by the template */
$("#listView").kendoListView(
{
template: kendo.template(overview_template),
dataSource: overview_datasource,
selectable: true,
change: function()
{
var idx = this.select().index();
var item = this.dataSource.view()[idx];
alert(item.name);
},
dataBound: function()
{
//Fires when the list view has received data from the data source and it is already rendered.
// get a reference to the list view widget
var listView = $("#listView").data("kendoListView");
// selects first list view item
listView.select(listView.element.children().first());
}
});
});
您确定事件已触发两次吗?在这里测试它:
overview\u content
事件处理程序在做什么?我看到的是,它被触发一次,然后change
被触发。而如果使用trigger
则不会调用函数overview\u content
。加载JSFIDLE页面时,只会触发一次更改事件(这来自数据绑定函数),单击按钮下方列表视图中的项目时,会触发两次事件,因为我有两次弹出的表演。为了进行测试,我们可以删除overview_内容,因为这是一个空函数。您的意思是,在我的JSFIDLE中,当您单击performance时,会收到两次警报吗?我只得到一次!您使用的是哪个操作系统/浏览器?您是对的,但是尝试用console.log替换您的警报,然后它只显示一次(有趣!)检查这里我从未使用过kendoConsole.log
,我总是使用本机console.log
,但为了方便起见,请检查此项(之前评论中的链接是错误的-与原始回复相同-)没错,我就是这么认为的!alert
在JavaScript+浏览器中非常特殊:这实际上是唯一的方法(afaik)要停止正在运行的线程的执行
其他机制只是正在运行的线程和作为事件处理程序在后台发生的任务之间的同步。
[
{
"name": "Performance",
"led": "ok_green"
}
]