简体   繁体   English

淘汰赛:如何将Checkbox的“ checked”值传递给Click处理函数?

[英]Knockout: How to pass the Checkbox “checked” value to a Click handler function?

I want to send "true/false" value to a click handler, depending if the checkbox is checked or not. 我想将“ true / false”值发送给点击处理程序,具体取决于是否选中了该复选框。

  • On check, it should send a 3rd parameter (isChecked) with value "true". 检查时,它应发送值为“ true”的第三个参数(isChecked)。
  • On uncheck, it should send a 3rd parameter (isChecked) with value "false". 取消选中后,它将发送第三个参数(isChecked),其值为“ false”。

I'm sure this is really easy, but I'm having a hard time figuring it out. 我敢肯定这真的很容易,但是我很难弄清楚。 Here is the input element: 这是输入元素:

<input class="cards-view--item-checkbox pull-right" type="checkbox"
 data-bind="value: universalParcelId, checked: $parent.isChecked, checkedValue: true, 
 click: function(data, event, isChecked) { 
 return $root.addUPIDtoArray(data, event, $parent.isChecked()) }">

Click handler: 点击处理程序:

addUPIDtoArray: function (data, event, isChecked) {
    var self = this;

    self.isChecked = ko.observable();

    // If checked
    if(isChecked()) {
        self.upIDArray.push(data.universalParcelId);
        self.upIDWithIndexArray.push({
            universalParcelID: data.universalParcelId,
            searchResultIndex: data.searchResultIndex
        });

        // If unchecked
    } else if(!isChecked()) {
        // remove from array
    }

    return true; // allow the default "click" action, which is checking the box with a "check"
},

I thought i could use the "event" parameter, but for some reason it is coming through as a jQuery.event, instead of a regular DOM event. 我以为我可以使用“事件”参数,但是由于某种原因,它作为jQuery.event而不是常规的DOM事件来传递。 So I decided for the 3rd parameter. 因此,我决定使用第三个参数。 But it just doesn't work like this: gives error $parent.isChecked is not a function 但这不是这样的:给出错误$parent.isChecked is not a function

Any ideas? 有任何想法吗?

Unless you need to distinguish a click from some other way of setting the variable in the checked binding, you don't want a click handler. 除非您需要将单击与在已checked绑定中设置变量的其他方式区分开,否则您就不需要单击处理程序。 You just want to subscribe to the variable, which will execute your function whenever its value changes. 您只想subscribe变量,变量值更改时将执行该函数。

You've written your click binding as if adding the parameter to the parameter list will make Knockout know what to pass it. 您已经编写了click绑定,就好像将参数添加到参数列表将使Knockout知道要传递什么一样。 You'll want to re-think that. 您将需要重新考虑。 Generally, it's best to write your click handler as a member of your ViewModel and just make the binding like click: methodName . 通常,最好将单击处理程序编写为ViewModel的成员, click: methodNameclick: methodName那样进行绑定。

Below is an example of a click binding on a checkbox. 以下是复选框上的单击绑定的示例。 There's an interval toggling the checkbox each second. 有一个间隔每秒切换复选框。 That won't trigger the click binding. 那不会触发点击绑定。

There is also a subscription that counts the times the value has changed, and what the last value was. 还有一个订阅,它计算值更改的时间以及最后一个值是多少。

 vm = { box: ko.observable(true), changes: ko.observable(0), lastChange: ko.observable(''), stuff: ko.observableArray(), doThing: function() { vm.stuff.push(vm.box() ? 'checked' : 'not'); return true; } }; vm.box.subscribe(function(newValue) { vm.changes(vm.changes() + 1); vm.lastChange(newValue ? 'checked' : 'not'); }); ko.applyBindings(vm); // This will change the checkbox without firing the click setInterval(function() { vm.box(!vm.box()); }, 1000); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <input type="checkbox" data-bind="checked: box, click: doThing" /> <div>Changes: <span data-bind="text:changes"></span>, last:<span data-bind="text:lastChange"></span> <div data-bind="foreach:stuff"> <div data-bind="text: $data"></div> </div> 

I got it to work by utilizing $element.checked and passing that as a parameter to my click handler function 我通过利用$element.checked并将其作为参数传递给我的点击处理程序函数来使其工作

<input style="display: none;"  class="cards-view--item-checkbox pull-right" type="checkbox"
data-bind="value: universalParcelId, checked: $parent.isChecked, click: function(data, event) { 
return $root.addUPIDtoArray($element.checked, data, event) }">

It might not be "best practice" but it works! 它可能不是“最佳实践”,但可以! What objections are there to doing it this way? 这样做有什么反对意见?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM