tags:

views:

1301

answers:

5

I have three radio buttons with same name and different values.When I click the third radio button the checkbox and textbox going to be disabled.but when I choose other two radio buttons it must be show.I need the help in Jquery.Thanks in advance....

<form name="checkuserradio">
    <input type="radio" value="1" name="userradiobtn" id="userradiobtn"/> 
    <input type="radio" value="2" name="userradiobtn" id="userradiobtn"/>
    <input type="radio" value="3" name="userradiobtn" id="userradiobtn"/>
    <input type="checkbox" value="4" name="chkbox" />
    <input type="text" name="usertxtbox" id="usertxtbox" />
</form>
A: 

I'm guessing you probably want to bind a "click" event to a number of radio buttons, read the value of the clicked radiobutton and depending on the value, disable/enable a checkbox and or textbox.

function enableInput(class){
    $('.' + class + ':input').attr('disabled', false);
}

function disableInput(class){
    $('.' + class + ':input').attr('disabled', true);
}

$(document).ready(function(){
    $(".changeBoxes").click(function(event){
        var value = $(this).val();
        if(value == 'x'){
            enableInput('foo'); //with class foo
            enableInput('bar'); //with class bar
        }else{
            disableInput('foo'); //with class foo
            disableInput('bar'); //with class bar
        }
    });
});
Niels Bom
I haven't made this work with the supplied HTML, but that's fairly easy imho.
Niels Bom
+2  A: 

HTML

<span id="radiobutt">
  <input type="radio" name="rad1" value="1" />
  <input type="radio" name="rad1" value="2" />
  <input type="radio" name="rad1" value="3" />
</span>
<div>
  <input type="text" id="textbox1" />
  <input type="checkbox" id="checkbox1" />
</div>

Javascript

  $("#radiobutt input[type=radio]").each(function(i){
    $(this).click(function () {
     if(i==2) { //3rd radiobutton
      $("#textbox1").attr("disabled", "disabled"); 
      $("#checkbox1").attr("disabled", "disabled"); 
     }
     else {
      $("#textbox1").removeAttr("disabled"); 
      $("#checkbox1").removeAttr("disabled"); 
     }
      });

  });
o.k.w
@vinothkumar: Oops, I started coding the answer before seeing your sample code snippet
o.k.w
@vinothkumar: Matt's answer is more efficient. Take mine as a not-so-good example.
o.k.w
Here where we can define i value.
vinothkumar
@vinothkumar `i` is passed in from `function(i)` via JQuery's `each()`. `i==2` is used to access the 3rd radio button. Refer to my codes.
o.k.w
+2  A: 

Not really necessary, but a small improvement to o.k.w.'s code that would make the function call faster (since you're moving the conditional outside the function call).

$("#radiobutt input[type=radio]").each(function(i) {
    if (i == 2) { //3rd radiobutton
        $(this).click(function () {
            $("#textbox1").attr("disabled", "disabled");
            $("#checkbox1").attr("disabled", "disabled");
        });
    } else {
        $(this).click(function () {
            $("#textbox1").removeAttr("disabled");
            $("#checkbox1").removeAttr("disabled");
        });
    }
});
Matt Huggins
@Matt, that's an improvement indeed :)
o.k.w
+5  A: 

I'm not sure why some of these solutions use .each() - it's not necessary.

Here's some working code that disables if the 3rd checkbox is clicked, otherwise is removes the disabled attribute.

Note: I added an id to the checkbox. Also, remember that ids must be unique in your document, so either remove the ids on the radiobuttons, or make them unique

$("input:radio[name='userradiobtn']").click(function() {
    var isDisabled = $(this).is(":checked") && $(this).val() == "3";
    $("#chkbox").attr("disabled", isDisabled);
    $("#usertxtbox").attr("disabled", isDisabled);
});
ScottE
Thanks verymuch...I will workout and told the answer
vinothkumar
@ScottE: `each()` was used because we want to get the 3rd radio button. We can use `$("input[type=radio]:eq(2)")` too. Your method identify the radio button by value, which of course is valid too. :)
o.k.w
if the user first select the checkbox and enter some text in textbox and then if he select the radio button the checked checkbox must be unchecked and the textbox going to be cleared?How can I do this...I ask these questions because I am anewbie...Thanks in advance..
vinothkumar
@vinothkumar - with that requirement in mind I probably would not have set up the js as above, but in this case, just check is the element is disabled and call $("#usertxtbox").val("") on the I would have
ScottE
A: 

I would of done it slightly different

 <input type="radio" value="1" name="userradiobtn" id="userradiobtn" />   
 <input type="radio" value="2" name="userradiobtn" id="userradiobtn" />    
 <input type="radio" value="3" name="userradiobtn" id="userradiobtn" class="disablebox"/>   
 <input type="checkbox" value="4" name="chkbox" id="chkbox" class="showbox"/>    
 <input type="text" name="usertxtbox" id="usertxtbox" class="showbox" />

Notice class attribute

 $(document).ready(function() {      
    $('.disablebox').click(function() {
        $('.showbox').attr("disabled", true);           
    });
});

This way should you need to add more radio buttons you don't need to worry about changing the javascript

Si Philp