选中复选框时,禁用单选按钮组jquery

时间:2013-11-14 09:06:35

标签: javascript jquery html checkbox

嗨,你们所有我在这里得到了我的代码,我认为是正确的,它应该工作正常,但它没有。我有一个单选按钮组和一个复选框。单选按钮的值有一个值用作一个金额,复选框只用于启用文本框,允许用户输入他们想要的金额。通过检查是否已经检查它是否可以启用文本框。我想禁用收音机启用复选框时启用组。 这是我的页面实际上看起来像的截图

enter image description here

这是我的HTML

     <label class="radio-inline" style="margin-left:15%;" >
                            <input type="radio" id="amount_suggest_1" name="montantdon" value="50 000" onFocus="this.blur()"  >
                         50 000</label>
    <label class="radio-inline"><input type="radio" id="amount_suggest_2" name="montantdon" value="25 000" onfocus="this.blur()" >25 000</label>
    <label class="radio-inline"><input type="radio" id="amount_suggest_3" name="montantdon" value="10000" onfocus="this.blur()" >10 000</label>
    <label class="radio-inline"><input type="radio" id="amount_suggest_4" name="montantdon" value="5000" onfocus="this.blur()" >5000</label>
    <label class="radio-inline lastradio"><input type="radio" id="amount_suggest_5" name="montantdon" value="1000" onfocus="this.blur()"  checked>1 000</label>

                        <div class="row" style="padding-left:20px;">
                            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-5" style="width:200px;width:200px;margin:auto;margin-top:2%;">
                                <div class="input-group input-group-amount" >
                                <label class=""><input type="checkbox" name="autresdon" id="autresdon" />&nbsp;Autres</label>
                                    <div style="display:inline-block;vertical-align:top;"><input type="text" style="width:50px;" name="amountentered" id="amountentered" disabled /></div>
                                    <div style="display:inline-block;vertical-align:top;">
                                  <div class="input-group-btn" style="position:relative;">
                                      <button type="button" class="btn btn-default btn-lg dropdown-toggle currency-current" data-toggle="dropdown"><span class="currency" id="currency">EUR-€</span> <span class="caret"></span></button>

这是我的jquery代码

          $('#autresdon').click(function(){//on click the checkbox
      if($('#autresdon').is(':checked'))
                {
                $('#amountentered').prop("disabled",false);//enables textbox
                }
                else
                {
                $('#amountentered').prop("disabled",true);//on unset checkbox disable textbox
                }
      });

      //disabling radio group by using their class as selector
             $('.montantdon ').click(function(){
             if($('#autresdon').is(':checked'))
             {
                $('#autresdon').prop("checked",false);
                }
      });

我不希望发布这两个数据(无线电的价值和手动输入的金额)。我可以解决这个问题吗?谢谢

1 个答案:

答案 0 :(得分:5)

如果我正确理解了这个问题,我建议:

// binds the change-handler:
$('#autresdon').change(function(){
    // sets the 'disabled' property to false (if the 'this' is checked, true if not):
    $('.radio-inline').prop('disabled', !this.checked);
// triggers the change event (so the disabled property is set on page-load:
}).change();

顺便说一句,使用JavaScript(以及尤其是中的,完全没有必要)远离内嵌/突出的JavaScript。使用onFocus / onfocusonchangeonclick等会导致维护噩梦并创建一些非常(并且,再次,不必要)不整洁 HTML

参考文献:

相关问题