【问题标题】:Why isn't my checkbox checking with jquery?为什么我的复选框没有用 jquery 检查?
【发布时间】:2020-11-11 03:47:59
【问题描述】:

我在我的项目(Magento 2)中使用 kojs,并且我正在调用一个显示和隐藏输入框的方法。该功能正在工作(该框根据点击显示/隐藏),但复选框没有得到它的复选标记。

知道是什么原因造成的吗?

谢谢

 <input class="sf-other-checkbox" type="checkbox" id="other" value="Other" data-bind="click: sfOther">
        <label style="margin-bottom: 0.5rem;" for="other">Other</label>
     sfOther: function () {
            $(this.sfOtherInput).toggle(this.sfOtherCheckbox.checked);
        }

【问题讨论】:

  • Magento 是否会在您绑定操作时禁用默认操作?
  • 您能否添加更多代码以便我们可以复制问题
  • @barmer 我不知道。在我添加数据绑定之前它正在工作,所以可能,但我不知道是 jquery 还是 kojs
  • @asmoun 这里没有更多相关代码
  • @webmaster jsfiddle.net/ahzuogmr 是这样的,复选框没有像你描述的那样隐藏和显示,你可以正常勾选和取消勾选,我错过了什么?

标签: html jquery magento


【解决方案1】:

原来是 Magento 2 的 ko 绑定问题。

我必须在我的 div 上使用这种语法

 <input class="sf-other-checkbox" type="checkbox" id="other-category" ko-checked="showSfOther" />

这会切换我在 js 中初始化的变量。 然后在我想要显示/隐藏的 div 上,我添加了这个

<div class="field sf-other-input required" if="showSfOther">

这一切都很好地切换

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多