Checking and unchecking the boxes in the Mobile jQuery dialog box

I tried this code to achieve what I want to do. This worked when I tried it in my regular HTML file, but when I tried it on my JQuery Mobile page, the code did not work for me. Are there different ways or code to select jQuery Mobile checkboxes?

Here is the code I tried:

JAVASCRIPT:

<script> function SetAllCheckBoxes(FormName, FieldName, CheckValue) { if(!document.forms[FormName]) return; var objCheckBoxes = document.forms[FormName].elements[FieldName]; if(!objCheckBoxes) return; var countCheckBoxes = objCheckBoxes.length; if(!countCheckBoxes) objCheckBoxes.checked = CheckValue; else // set the check value for all check boxes for(var i = 0; i < countCheckBoxes; i++) objCheckBoxes[i].checked = CheckValue; } 

HTML

 <form method="GET" name="myForm" onsubmit="return false;"> <label for="myCheckbox1"> <input type="checkbox" name="myCheckbox" value="1" id="myCheckbox1"> I like Britney Spears </label> <br> <label for="myCheckbox2"><input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2"> I like Hillary Duff </label> <br> <label for="myCheckbox3"><input type="checkbox" name="myCheckbox" value="3" id="myCheckbox3"> I like Mandy Moore </label> <br> <input type="button" onclick="SetAllCheckBoxes('myForm', 'myCheckbox', true);" value="I like them all!"> &nbsp;&nbsp; <input type="button" onclick="SetAllCheckBoxes('myForm', 'myCheckbox', false);" value="I don't like any of them!"> 

+7
source share
3 answers

I did not know that the flags of JQuery Mobile needed to be updated after removing / checking them through jQuery or Javascript. Here is the code:

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

http://api.jquerymobile.com/checkboxradio/#method-refresh

+22
source

Try the following:

 function SetAllCheckBoxes(FormName, CheckValue){ $.each($("#"+FormName+" input[type=checkbox]"), function(){ $(this).attr("checked",CheckValue).checkboxradio("refresh"); }); } 

I deleted FieldName because your function was called SetAllCheckBoxes, only your input fields match. You just need to indicate what shape and condition your flags are.

+4
source

To switch / update checkboxes in jQuery mobile, you need to use .prop, not .attr.

$("input[type='checkbox']").prop("checked",true).checkboxradio("refresh");

0
source

All Articles