【问题标题】:Data-bind disable attribute doesn't work in a button inside a foreach数据绑定禁用属性在 foreach 内的按钮中不起作用
【发布时间】:2016-03-21 12:33:59
【问题描述】:

我正在使用一个表格,其中有一个数据表和一个用于打开模式的按钮。我需要将此按钮设置为始终启用;我尝试使用下面的代码,但没有成功。

ko.applyBindings({
  editarTexto: function(data) {
    alert("msg")
  },
  items: [{}, {}, {}]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<fieldset data-bind="disable: true">
  <table class="table">
    <thead>
      <tr>
        <th class="col-lg-1 ">Texto</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <button type="button" class="form-control" data-bind="click: function() { $root.editarTexto($data) }, disable: false">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true" data-bind="disable: false">&times;</span>
          </button>
        </td>
      </tr>
    </tbody>
  </table>
</fieldset>

谁能帮帮我?

【问题讨论】:

标签: javascript knockout.js


【解决方案1】:

您掩盖了标题中的实际问题,并在代码中四处游荡。然而,代码本身表明您的问题可以解释为:

如何在具有disabled 属性的字段集中仅启用某些buttons?

因此复制可以小得多:

<fieldset disabled>
   <input> <!-- should be disabled -->
   <button>x</button> <!-- should be ENabled, but isn't -->
</fieldset>

答案是:你不能。唯一的解决方案是禁用fieldset 并设置除按钮之外的所有后代元素disabled


作为参考,您可以从the enable/disable binding source file 中看到,除了在相关元素上设置disabled 属性之外,它没有做任何事情。


参考见MDN条目fieldset disabled attribute

如果设置了此布尔属性,则作为其后代的表单控件(其第一个可选元素的后代除外)将被禁用,即不可编辑。他们不会收到任何浏览事件,例如鼠标点击或与焦点相关的事件。浏览器通常会将此类控件显示为灰色。


有关参考,另请参阅the AngularJS version of your question

【讨论】:

  • 我觉得使用form 属性,指向一个未被禁用的表单,应该保持我的按钮启用。或者更好的是,带有type="button" 的按钮。但事实并非如此。您可以使用 ARIA role="button" 保留启用的“按钮”。在此处查看答案:stackoverflow.com/a/55155649/2444959
【解决方案2】:

根据spec,你可以把你的按钮放在&lt;legend&gt;里面。

<fieldset disabled>
  <legend>
    Legend
    <!-- This is ENABLED -->
    <button onclick="console.log('click')">Enabled</button>
  </legend>
  <input> <!-- this is disabled -->
  <button>disabled</button> <!-- this is disabled -->
</fieldset>

【讨论】:

    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 2017-12-30
    相关资源
    最近更新 更多