首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果未选中复选框,则禁用Button

如果未选中复选框,则禁用Button
EN

Stack Overflow用户
提问于 2017-11-29 01:35:32
回答 2查看 63关注 0票数 0

下面的表结构是从其中的rest调用中生成的输入复选框,在创建表之前,我希望禁用一个静态按钮,除非单击了任何复选框。

代码语言:javascript
复制
<div id="divbuttons">
  <button id="Reject" type="button">Reject</button>
  <button id="Approve" type="button">Approve</button>
</div>

<table id="GetItems" cellspacing="12">
  <tbody>
   <tr style="background-color: rgb(204, 204, 204);">
    <td style="width: 349px;"><strong>Select</strong> 
       <input name="chk" type="checkbox" value="35">
    </td>
    <td style="width: 211px;">&nbsp;</td>
    <td style="width: 396px;"><strong>Requester: </strong>Test user</td>
    <td style="width: 149.2px;"><strong>Raised Date:</strong> 29/11/2017</td>
    <td style="width: 806.8px;" colspan="3">&nbsp;</td></tr><tr>
    <td style="width: 349px;"><strong>Item Requested For:<br>:</strong>Desktops</td>
    <td style="width: 211px;"><strong>Bomb<br>&nbsp;</strong>ITSS</td>
    <td style="width: 396px;"><strong>Department:</strong><br>ITSS Infrastructure</td>
    <td style="width: 149.2px;"><strong>Job Title:</strong><br>Consultant</td>
    <td style="width: 376.8px;"><strong>Phone Number:</strong><br>032394</td>
    <td style="width: 213px;"><strong>Request Type</strong><br>:New Allocation</td>
    <td style="width: 217px;"><strong>Replacement Type:<br></strong>New Allocation</td>
   </tr>
  </tbody>
</table>

如何使用jQuery实现这一目标?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-29 03:01:19

给你一个解决办法

代码语言:javascript
复制
$('#GetItems tbody').html(`<tr style="background-color: rgb(204, 204, 204);">
    <td style="width: 349px;"><strong>Select</strong> 
       <input name="chk" type="checkbox" value="35">
    </td>
    <td style="width: 211px;">&nbsp;</td>
    <td style="width: 396px;"><strong>Requester: </strong>Test user</td>
    <td style="width: 149.2px;"><strong>Raised Date:</strong> 29/11/2017</td>
    <td style="width: 806.8px;" colspan="3">&nbsp;</td></tr><tr>
    <td style="width: 349px;"><strong>Item Requested For:<br>:</strong>Desktops</td>
    <td style="width: 211px;"><strong>Bomb<br>&nbsp;</strong>ITSS</td>
    <td style="width: 396px;"><strong>Department:</strong><br>ITSS Infrastructure</td>
    <td style="width: 149.2px;"><strong>Job Title:</strong><br>Consultant</td>
    <td style="width: 376.8px;"><strong>Phone Number:</strong><br>032394</td>
    <td style="width: 213px;"><strong>Request Type</strong><br>:New Allocation</td>
    <td style="width: 217px;"><strong>Replacement Type:<br></strong>New Allocation</td>
   </tr>`);
   
$('#GetItems').on('change', 'input[name=chk]', function(){
  if($(this).is(':checked')) {
    $('#Reject').removeAttr('disabled');
    $('#Approve').removeAttr('disabled');
  } else {
    $('#Reject').attr('disabled', true);
    $('#Approve').attr('disabled', true);
  }
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divbuttons">
  <button id="Reject" type="button" disabled>Reject</button>
  <button id="Approve" type="button" disabled>Approve</button>
</div>

<table id="GetItems" cellspacing="12">
  <tbody>
   
  </tbody>
</table>

我认为tabletbody是静态的,行是动态添加的。

由于复选框是动态生成的,因此需要委托更改事件。

希望这个解决方案能帮到你。

票数 0
EN

Stack Overflow用户

发布于 2017-11-29 01:44:06

试试下面的代码:JSFiddle

代码语言:javascript
复制
$("input[name='chk']").on("change",function(){
   if(!$(this).is(':checked')) {
     $('#Reject').prop('disabled', true);
     $('#Approve').prop('disabled', true);
   } else {
    $('#Reject').prop('disabled', false);
    $('#Approve').prop('disabled', false);
   }
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47543684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档