Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking and unchecking radio buttons with Jquery Mobile

I can't check a set of checkboxes programatically with jquery mobile, I have the following code:

<div data-role="fieldcontain"  id="div_radio" class="radiogroup">
    <fieldset data-role="controlgroup">
        <input type="radio" name="radio-pieces" id="radio-choice-1" value="3" checked="checked" />
        <label for="radio-choice-1">1 to 3</label>

        <input type="radio" name="radio-pieces" id="radio-choice-2" value="5"  />
        <label for="radio-choice-2">4 to 5</label>

        <input type="radio" name="radio-pieces" id="radio-choice-3" value="6"  />
        <label for="radio-choice-3">over 5</label>
    </fieldset>
</div>

If I do: $("input[type='radio']:last").attr("checked",true).checkboxradio("refresh"); everything works perfect, but none of this work at all:

$("input[type='radio']:first").attr("checked",true).checkboxradio("refresh");
$("input[type='radio']:eq(0)").attr("checked",true).checkboxradio("refresh");
$("input[type='radio']:eq(1)").attr("checked",true).checkboxradio("refresh");
$("input[type='radio']:eq(2)").attr("checked",true).checkboxradio("refresh");

How can I properly manipulate these elements? Unselecting all checkboxes also works fine:

$("input[type='radio']").attr("checked",false).checkboxradio("refresh");

It seems that the only checkbox working is the last one.

like image 492
danielrvt-sgb Avatar asked Jan 18 '13 23:01

danielrvt-sgb


People also ask

How we can check uncheck radio buttons in jQuery?

Answer: Use the jQuery prop() method You can use the jQuery prop() method to check or uncheck radio button dynamically such as on click of button or an hyperlink etc. The prop() method require jQuery 1.6 and above.

How do you check and uncheck a radio button?

To set a radio button to checked/unchecked, select the element and set its checked property to true or false , e.g. myRadio. checked = true . When set to true , the radio button becomes checked and all other radio buttons with the same name attribute become unchecked. Here is the HTML for the examples in this article.

How do you dynamically populate a radio button?

For creating dynamic RadioButton, we need to use android. view. ViewGroup. LayoutParams which configures the width and height of views and implements setOnCheckedChangeListener() method of RadioGroup class.

How do I deselect all radio buttons?

If you want to uncheck all the radio buttons and checkboxes you will need to add one single line (in bold): $('#clear-all').


1 Answers

They all work just fine. You just need to trigger refresh on all input radio in group.

$("input[type='radio']:first").attr("checked", "checked");
$("input[type='radio']").checkboxradio("refresh");

jsFiddle is here.

like image 100
peterm Avatar answered Nov 03 '22 14:11

peterm