nockoutjs radiobutton点击事件

发布于 2025-02-02 06:44:25 字数 832 浏览 1 评论 0原文

我有一些HTML:

<input name="Test" type="radio" value="yes" data-bind="checked: Page2.Traded, click: Traded_Yes" /> Yes
<input name="Test" type="radio" value="no" data-bind="checked: Page2.Traded, click: Traded_No" /> No

还有一些JavaScript:

    self.Traded_Yes = function(data) {
       $("#Page2Symbol").prop("disabled", false);
       return true;
    };

    self.Traded_Yes = function(data) {
       $("#Page2Symbol").prop("disabled", true);
       return true;
    };

在禁用ID:Page2Symbol和保存正确的数据方面,UI上的一切正常,但是UI在我单击任何一个收音机时都不会显示在Yes/no之间的广播按钮。我做返回true以允许默认单击操作,如下所示: https://knockoutjs.com/documentation/click-binding.html ,但是当我单击一个收音机时,UI不会翻转选择。有什么想法发生了什么事?

I have some html:

<input name="Test" type="radio" value="yes" data-bind="checked: Page2.Traded, click: Traded_Yes" /> Yes
<input name="Test" type="radio" value="no" data-bind="checked: Page2.Traded, click: Traded_No" /> No

And some Javascript:

    self.Traded_Yes = function(data) {
       $("#Page2Symbol").prop("disabled", false);
       return true;
    };

    self.Traded_Yes = function(data) {
       $("#Page2Symbol").prop("disabled", true);
       return true;
    };

Everything works fine on the UI in terms of disabling id: Page2Symbol, and saving the correct data however the UI doesn't show the radio button flipping between Yes/No when I click either radio. I doreturn true in order to allow the default click action as discussed here: https://knockoutjs.com/documentation/click-binding.html , however the UI does not flip the selection when I click either radio. Any ideas what is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2025-02-09 06:44:25

我无法重现您的问题,但是可能有一些很好的建议可以解决您可能遇到的问题:

在使用淘汰赛时,建议与DOM进行 All All 通过敲除绑定。为了禁用和启用按钮,有禁用enable绑定。以下是您使用它们的方式:

使用disable绑定:

您将计算属性添加到视图模型中,以根据交易。 >。

这样可以确保您不需要点击绑定,并且可以摆脱任何id引用。

const self = {};

self.Page2 = { Traded: ko.observable("yes") };
self.disablePage2Symbol = ko.pureComputed(() => self.Page2.Traded() === "yes");

ko.applyBindings(self);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<button data-bind="disable: disablePage2Symbol">Page 2 button</button>

<label> 
  <input type="radio" value="yes" data-bind="checked: Page2.Traded" /> Yes
</label>
<label> 
  <input type="radio" value="no" data-bind="checked: Page2.Traded" /> No
</label>

该问题的复制失败:

该片段表明您的原始代码应该起作用,即使这有点反图案。

const self = {};

self.Traded_Yes = function(data) {
   $("#Page2Symbol").prop("disabled", false);
   return true;
};

self.Traded_No = function(data) {
   $("#Page2Symbol").prop("disabled", true);
   return true;
};

self.Page2 = { Traded: ko.observable("yes") };

ko.applyBindings(self);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<button id="Page2Symbol">Page 2 button</button>

<input name="Test" type="radio" value="yes" data-bind="checked: Page2.Traded, click: Traded_Yes" /> Yes
<input name="Test" type="radio" value="no" data-bind="checked: Page2.Traded, click: Traded_No" /> No

I can't reproduce your issue, but might have some good advice to work around the issue you might have:

When working with knockout, it's advised to make all interaction with the DOM go through a knockout binding. For disabling and enabling a button, there are the disable and enable bindings. Here's how you use them:

Using the disable binding:

You add a computed property to your viewmodel that defines whether to disable based on the value of Traded.

This ensures you don't need that click-binding and can get rid of any id references.

const self = {};

self.Page2 = { Traded: ko.observable("yes") };
self.disablePage2Symbol = ko.pureComputed(() => self.Page2.Traded() === "yes");

ko.applyBindings(self);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<button data-bind="disable: disablePage2Symbol">Page 2 button</button>

<label> 
  <input type="radio" value="yes" data-bind="checked: Page2.Traded" /> Yes
</label>
<label> 
  <input type="radio" value="no" data-bind="checked: Page2.Traded" /> No
</label>

Failed reproduction of the issue:

This snippet shows that your original code should work, even though it's a bit of an anti-pattern.

const self = {};

self.Traded_Yes = function(data) {
   $("#Page2Symbol").prop("disabled", false);
   return true;
};

self.Traded_No = function(data) {
   $("#Page2Symbol").prop("disabled", true);
   return true;
};

self.Page2 = { Traded: ko.observable("yes") };

ko.applyBindings(self);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<button id="Page2Symbol">Page 2 button</button>

<input name="Test" type="radio" value="yes" data-bind="checked: Page2.Traded, click: Traded_Yes" /> Yes
<input name="Test" type="radio" value="no" data-bind="checked: Page2.Traded, click: Traded_No" /> No

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文