使用表格上的复选框更改值

问题描述 投票:0回答:2

我有此hbs代码:

<table>
  <tr>
    <th>A</th>
    <th>B</th>    
  </tr>
  {{#each data2}}
  <tr>
    <td>
      <input
      type='checkbox'
      id='nm_produk'
      class='product'
      name='data_{{no}}'
      value="{{no}}">
      <label>{{item}}</label>
    </td>
    <td>
      <input
      disabled type='text'
      placeholder='0'
      name='data_{{no}}'
      id='qtytbs'>
    </td>
  </tr>
  {{/each}}
</table>

和此脚本:

$(document).ready(function() {
    $(".product").change(function() {
        $next = $(this).closest('tr').find('[id=qtytb]');
        $next.prop('disabled', !this.checked);   
    });
});

当我选中复选框时,输入元素的同一行被启用,我们可以在上面写上文字。但是当我取消选中该复选框时,该值仍然存在最后一个值。当我取消选中复选框元素时,我想在同一行的输入元素上获取空白值并禁用它。

我该如何实现?

jquery html ajax dom
2个回答
0
投票

添加$ next.val('');仅在禁用复选框的情况下将其设置为代码。您可能需要编写一个if条件来设置空值。


0
投票

将您的JS代码更改为:

© www.soinside.com 2019 - 2024. All rights reserved.