javascript – 单击绑定选中复选框在Knockout中无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 单击绑定选中复选框在Knockout中无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2271字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 单击绑定选中复选框在Knockout中无法正常工作](/upload/InfoBanner/zyjiaocheng/756/70301469d4c54b4c82251482c3664d45.jpg)
我有一个复选框和文本框.我希望textbox为空,并且每当选中复选框时都不可编辑.
部分在那里我有禁用文本框时,文本框是未选中的作品不错,但因为我必须使用复选框的点击它结合和,只要我使用点击绑定排空部分不做工精细,它打破了复选框的行为和它成为不可点击.我有一个jsFiddle:http://jsfiddle.net/qK5Y3/16/
和下面的代码示例:
<input type="checkbox" id="emailTemplateSendAtTime" name="emailTemplateSendAtTime" data-bind="checked:SendAtTime, click:ClickSendAtTime"/>
<input type="text" style="width: 250px" id="emailTemplateSendAtTimeProperty" data-bind="value: SendAtTimeProperty, enable:SendAtTime"/>
这是我的js:
var ViewModel = function () {
this.SendAtTimeProperty = ko.observable("Something");
this.SendAtTime = ko.observable();
this.ClickSendAtTime = function () {
if (model.SendAtTime() == false) {
model.SendAtTimeProperty("");
}
};
};
ko.applyBindings(new ViewModel());
有什么建议?
解决方法:
两件事情:
>将模型添加为ClickSendAtTime的参数
>在ClickSendAtTime上返回true以避免取消该事件
var ViewModel = function () {
this.SendAtTimeProperty = ko.observable("Something");
this.SendAtTime = ko.observable();
this.ClickSendAtTime = function (model) {
if (model.SendAtTime() == false) {
model.SendAtTimeProperty("");
}
return true;
};
};
ko.applyBindings(new ViewModel());
另一种定义ClickSendAtTime的方法
this.ClickSendAtTime = function () {
if (this.SendAtTime() == false) {
this.SendAtTimeProperty("");
}
return true;
};
有关您需要返回true的原因的一些信息
Allowing the default action
By default, Knockout will prevent the event from taking any default action. For example if you use the event binding to capture the keypress event of an input tag, the browser will only call your handler function and will not add the value of the key to the input element’s value. A more common example is using the click binding, which internally uses this binding, where your handler function will be called, but the browser will not navigate to the link’s href. This is a useful default because when you use the click binding, it’s normally because you’re using the link as part of a UI that manipulates your view model, not as a regular hyperlink to another web page.
However, if you do want to let the default action proceed, just return true from your event handler function.
http://knockoutjs.com/documentation/event-binding.html
内容总结
以上是互联网集市为您收集整理的javascript – 单击绑定选中复选框在Knockout中无法正常工作全部内容,希望文章能够帮你解决javascript – 单击绑定选中复选框在Knockout中无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。