2016-05-15 65 views
0

我对jQuery很新,请把我当作新手。我有一个PHP表单,我有一个单选按钮。基于选择哪个收音机,我想禁用文本框。下面是场景:使用jQuery启用禁用文本框

单选按钮:现金和支票 文本框:支票号码和支票日期

默认情况下,文本框支票号码和支票日期设置为禁用和无线电现金被选中。

  1. 当用户点击检查收音机时,文本框检查号和检查日期必须启用,如果可能的话,它应该按要求制作。
  2. 当用户点击现金时,必须禁用文本框检查号和检查日期,必须删除必需的财产。

我加入的JQuery 1.12.3分钟,我的header.php

的header.php

!-- JQuery 1.12.3 JS --> 
<script src="../js/jquery-1.12.3.min.js"></script> 

MainPage.php

<tr> 
    <td class="col-md-4"><label class="control-label" for="payMode">Mode</label></td> 
    <td class="col-md-8"> 
     &nbsp; 
     <label class='radio-inline'><input type='radio' name='rbPayMode' id='bd' value='0' checked>Cash</label> 
     <label class='radio-inline'><input type='radio' name='rbPayMode' id='bd' value='1'>Cheque</label> 
    </td> 
</tr> 
<tr> 
    <td class="col-md-4"><label class="control-label" for="ChequeNo">Cheque No</label></td> 
    <td class="col-md-8"><input type="number" name="txtChequeNo" pattern="^[0-9]" min="0" step="1" class="form-control" placeholder="Enter Cheque No" disabled></td> 
</tr> 
<tr> 
    <td class="col-md-4"><label class="control-label" for="ChequeDate">Cheque Date</label></td> 
    <td class="col-md-8"><input type="date" name="txtChqueDate" class="form-control" placeholder="Enter Cheque Date" disabled></td> 
</tr> 

请帮助

回答

1

你可以像下面这样做。

$(':radio[name=rbPayMode]').change(function() { 
 
    var prop = this.value == 0; 
 
    $('input[name=txtChequeNo], input[name=txtChqueDate]').prop({ disabled: prop, required: !prop }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
     <td class="col-md-4"><label class="control-label" for="payMode">Mode</label></td> 
 
     <td class="col-md-8"> 
 
      &nbsp; 
 
      <label class='radio-inline'><input type='radio' name='rbPayMode' id='bd' value='0' checked>Cash</label> 
 
      <label class='radio-inline'><input type='radio' name='rbPayMode' id='bd' value='1'>Cheque</label> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td class="col-md-4"><label class="control-label" for="ChequeNo">Cheque No</label></td> 
 
     <td class="col-md-8"><input type="number" name="txtChequeNo" pattern="^[0-9]" min="0" step="1" class="form-control" placeholder="Enter Cheque No" disabled></td> 
 
    </tr> 
 
    <tr> 
 
     <td class="col-md-4"><label class="control-label" for="ChequeDate">Cheque Date</label></td> 
 
     <td class="col-md-8"><input type="date" name="txtChqueDate" class="form-control" placeholder="Enter Cheque Date" disabled></td> 
 
    </tr> 
 
</table>