如何获取有关复选框的数据已在 javascript 文件中选中

how to get data about checkbox is checked in javascript file

选中复选框时如何获取数据。

     MyClient =ko.observable(false);

这是 select 选项。

我用过

   self.MyClient.subscribe(function(newValue){ code goes here...})

但这不是我要找的东西,当我单击它时,它会执行我想要的操作,但是当我取消select 时同样会发生,因为在这两种情况下,MyClient 的值都是新的。

[已更新] 您可以通过将 required 属性(attr 绑定)绑定到布尔值来使字段成为必填字段。使用与绑定到您的复选框相同的布尔值。 CSS 在我的示例中需要时用红色勾勒出字段。

self = {
  myClient: ko.observable(false),
  myValue: ko.observable(''),
  message: function() {
    console.debug("Valu:", self.myValue());
    if (self.myClient() && !(self.myValue())) {
      return "Please enter value";
    }
  }
};

ko.applyBindings(self);
input[required] {
  outline: solid red 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="checkbox" data-bind="checked: myClient" />
<input data-bind="attr: {required: myClient}, value: myValue" /><span data-bind="text:message()"></span>