绑定时会触发基因敲除js select change事件 [英] knockoutjs select change event gets fired when binding

查看:102
本文介绍了绑定时会触发基因敲除js select change事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个剔除代码: http://jsfiddle.net/nickbuus/Rwabt/

问题在于,每当下拉选择框被填充时,更改事件就会被调用:

The problem is that whenever the dropdown select box gets filled the change event gets called:

 <select data-bind="options: $root.Categories, optionsText: 'categoryName', optionsValue:       'categoryId', value: CatId, optionsCaption: 'Vælg...', event: { change: $root.selectionChanged }"></select>

我该如何解决?

推荐答案

其中有很多损坏的未使用的代码

There was a lot of broken and unused code in there

http://jsfiddle.net/Rwabt/5/

此问题已得到纠正.进行更改实际上是出于两个原因,您在食物模型中使用了字符串,在类别模型("1" != 1)中使用了数字,类别2中不存在ID 2(仅1),两个食物均设置了啤酒类别( CatId 2).由于KO在选项列表中找不到这些值,因此将所选值重置为空.

This one is corrected. What actually did make it call change is for two reasons, you used string in the food model and numbers in category model ("1" != 1), Id 2 was not present in the categories (Only 1) the beer category was set for both food items (CatId 2). Since KO could not find these in the options list it reseted the selected value to null.

那就是说,请看一下我的小提琴,以了解更多的KO做事方式

That said please have a look at my fiddle to see a more KO way of doing things

这篇关于绑定时会触发基因敲除js select change事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆