如何在knockout js中实现checkbox事件 [英] how to implement checkbox event in knockout js
问题描述
大家好,
我确实要求在使用knockout js点击复选框时执行操作。在我的场景中,我最初通过为相应的实体创建8个对象来显示8行设计代码。
因此,当用户输入第一行数据时,我需要获取这些值并将其放入剩余的7中分别是行字段。所以为此我在顶部取了一个复选框,当它被点击时,我在复选框的点击事件中写了相应的代码。
它工作正常,但是当再次检查时,我需要从存在数据的所有7行中删除数据。我的代码如下。
设计代码: -
< input type =checkboxid =checkboxiddata-bind =click:ApplyToAll,enable:EnableApplyAll/>
在相应型号中: -
var self = this;
self.chkValue = ko.observable(false);
self.lines = ko.observableArray([new SupplierLine(),new SupplierLine(),new SupplierLine(),new SupplierLine(),new SupplierLine(),new SupplierLine(),new SupplierLine(),new SupplierLine()]);
Hi all,
i do have a requirement that to perform an operation when checkbox is clicked using knockout js. In my scenario initially i am displaying 8 lines of design code as same by creating 8 objects to that corresponding entity.
so when ever user enteres 1st line data i need to get those values and placed into remaining 7 lines fields respectively. so for that i have taken one checkbox at the top and when it is clicked i have written corresponding code in the click event of checkbox.
it is working fine but when ever again checked i need to remove the data from all 7 lines where ever the data is present. my code is as follows.
Design Code:-
<input type="checkbox" id="checkboxid" data-bind="click: ApplyToAll,enable: EnableApplyAll" />
In corresponding Model:-
var self = this;
self.chkValue = ko.observable(false);
self.lines = ko.observableArray([new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine(), new SupplierLine()]);
self.EnableApplyAll = ko.computed(function () {
var h = self.lines()[0].Height();
var l = self.lines()[0].Length();
return h != "" && l != "";
});
self.ApplyToAll = function(){
self.chkValue( !self.chkValue());
if(self.chkValue()){
var h;
var l;
var t;
var m;
$ .each(self.lines(),function(ind,ln){
if(ind == 0){
h = ln.Height();
l = ln.Length();
t = ln .strThick();
m = ln.strUnits();
}
else if(ind!) = 0){
ln.Height(h);
ln.Length(l);
ln.strThick(t);
ln.strUnits(m);
if(ind == 7){
self.chkValue(!self.chkValue()) ;
}
}
});
}
else {
$ .each(self.lines(),function(ind,ln){
if(ind!= 0){
ln.Height();
ln.Length();
ln.strUnits();
ln.strThick();
}
});
self.chkValue(!self.chkValue() );
}
};
};
但是当再次检查它没有进入其他部分,并且数据没有删除。请帮我解决这个问题,或者我在代码中写错了,请告诉我。
self.ApplyToAll = function () {
self.chkValue(!self.chkValue());
if (self.chkValue()) {
var h;
var l;
var t;
var m;
$.each(self.lines(), function (ind, ln) {
if (ind == 0) {
h = ln.Height();
l = ln.Length();
t = ln.strThick();
m = ln.strUnits();
}
else if (ind != 0) {
ln.Height(h);
ln.Length(l);
ln.strThick(t);
ln.strUnits(m);
if (ind == 7) {
self.chkValue(!self.chkValue());
}
}
});
}
else {
$.each(self.lines(), function (ind, ln) {
if (ind != 0) {
ln.Height("");
ln.Length("");
ln.strUnits("");
ln.strThick("");
}
});
self.chkValue(!self.chkValue());
}
};
};
but when ever again checked it is not coming into else part, and the data is not removing. please help me to solve this, or did i write any mistake in my code, plz let me know.
推荐答案
.each(self.lines(),function( ind,ln){
if(ind == 0){
h = ln.Height();
l = ln.Length() ;
t = ln.strThick();
m = ln.strUnits();
}
else if(ind!= 0){
ln.Height(h);
ln.Length( l);
ln.strThick(t);
ln.strUnits(m);
if (ind == 7){
self.chkValue(!self.chkValue());
}
}
});
}
else {
.each(self.lines(), function (ind, ln) {
if (ind == 0) {
h = ln.Height();
l = ln.Length();
t = ln.strThick();
m = ln.strUnits();
}
else if (ind != 0) {
ln.Height(h);
ln.Length(l);
ln.strThick(t);
ln.strUnits(m);
if (ind == 7) {
self.chkValue(!self.chkValue());
}
}
});
}
else {
>
.each(self.lines(),function(ind,ln){
if(ind!= 0){
ln.Height();
ln.Length();
ln.strUnits();
ln.strThick();
}
});
self.chkValue(!self.chkValue());
}
};
};
但是再次检查时它是没有进入其他部分,数据没有删除。请帮我解决这个问题,或者我在代码中写错了,请告诉我。
.each(self.lines(), function (ind, ln) {
if (ind != 0) {
ln.Height("");
ln.Length("");
ln.strUnits("");
ln.strThick("");
}
});
self.chkValue(!self.chkValue());
}
};
};
but when ever again checked it is not coming into else part, and the data is not removing. please help me to solve this, or did i write any mistake in my code, plz let me know.
这篇关于如何在knockout js中实现checkbox事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!