admin管理员组

文章数量:1320661

hi to you all i got my code here which i think is right and which is supposed to work fine but it doesn't.i have a radio button group and a checkbox.each of the radio button has a value which will be used as an amount and the checkbox is there only to enable the textbox that will allow the users to enter the amount they want.i made it possible to enable the textbox by checking if it has been checked first.i want to disable the radio group while the checkbox is enabled. here is a screenshot of what my page actually looks like

here is my 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>

and here is my jquery code

          $('#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);
                }
      });

i dont want both data(value of the radios and amount entered manually) to be posted.how can i solve this issue? thanks

hi to you all i got my code here which i think is right and which is supposed to work fine but it doesn't.i have a radio button group and a checkbox.each of the radio button has a value which will be used as an amount and the checkbox is there only to enable the textbox that will allow the users to enter the amount they want.i made it possible to enable the textbox by checking if it has been checked first.i want to disable the radio group while the checkbox is enabled. here is a screenshot of what my page actually looks like

here is my 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>

and here is my jquery code

          $('#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);
                }
      });

i dont want both data(value of the radios and amount entered manually) to be posted.how can i solve this issue? thanks

Share Improve this question asked Nov 14, 2013 at 9:06 Lilz Votca LoveLilz Votca Love 811 gold badge2 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

If I understand the question correctly, I'd suggest:

// 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();

Incidentally, with JavaScript (and especially in jQuery, in which it bees entirely unnecessary) stay away from in-line/obtrusive JavaScript. using onFocus/onfocus, onchange, onclick etc makes for a maintenance nightmare and creates some terribly (and, again, unnecessarily) untidy HTML.

References:

  • change().
  • prop().

本文标签: javascriptdisable radio button group when checkbox is checked jqueryStack Overflow