使用bootstrap-switch切换禁用输入框

时间:2016-12-12 23:07:58

标签: jquery html css twitter-bootstrap-3 bootstrap-switch

当我使用bootstrap-switch按钮切换时,我正在尝试禁用/启用表格中的输入框。

<table class="table .table-striped">
  <thead>
    <tr>
      <th>Number</th>
      <th>Inputbox</th>
      <th>checkbox</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td> 1 </td>
      <td><input id="some_value" type="number" class="form-control input-md" value=""></td>
      <td><input data-size="small" type="checkbox" id="constraint_checkbox" ></td>
    </tr>
    <tr>
      <td> 2 </td>
      <td><input id="some_value" type="number" class="form-control input-md" value=""></td>
      <td><input data-size="small" type="checkbox" id="constraint_checkbox" ></td>
    </tr>
    <tr>
      <td> 3 </td>
      <td><input id="some_value" type="number" class="form-control input-md" value=""></td>
      <td><input data-size="small" type="checkbox" id="constraint_checkbox" ></td>
    </tr>
  </tbody>
</table>

- jQuery方法1:这根本不起作用。

$('#constraint_checkbox').on('switchChange', function(event, state) {
    $(this).prop('enable', true);
  })

- 方法2:这只禁用第一个

 $(selector).on('switchChange.bootstrapSwitch', function(event, state) {
     if ($(selector).is(':checked') == false){
       $('#some_value').val('').prop('disabled', true);
      }
     else {
       $('#some_value').val('').prop('disabled', false);
      }
 });

1 个答案:

答案 0 :(得分:2)

您可以使用它来切换disabled输入的some_value属性:

$('#constraint_checkbox').on('change', function(event, state) {
  var status = $('#some_value').prop('disabled');
  $('#some_value').prop('disabled', !status);
});