Saturday, 15 September 2012

javascript - Difficulty inserting variable into jquery attribute identification -



javascript - Difficulty inserting variable into jquery attribute identification -

i'm trying jquery observe if text box in same grouping checked. code below shows how i'm trying retrieve grouping name when advanced box checked , utilize see if accompanying basic box checked. problem "basictrue" assigned "undefined", regardless of status of basic checkbox.

<div id="boxes"> <input style="text-align:center;" type="checkbox" name="group1" value="basic"> <input style="text-align:center;" type="checkbox" name="group1" value="advanced"> <input style="text-align:center;" type="checkbox" name="group2" value="basic"> <input style="text-align:center;" type="checkbox" name="group2" value="advanced"> </div> $("#boxes").contents().find(":checkbox").bind('change', function(){ val = this.checked; var $obj = $(this); if($obj.val()=="advanced"){ var grouping = $obj.attr("name"); var basictrue = $('input[name=group][value="basic"]').prop("checked"); if(basictrue) { //do stuff } else { $obj.attr('checked', false); } }

this code proof of concept used prove code formatted way works, homecoming status of "basic" checkbox in "group1".

var basictrue = $('input[name="group1"][value="basic"]').prop("checked");

i know variable "group" beingness given right name: group1 example. there reason why using variable in code wouldn't work?

those variables, , need concentenated string in selector, so:

$('input[name="' + grouping + '"][value="basic"]').prop("checked");

a simplified version:

$("#boxes input[type='checkbox']").on('change', function(){ var bt = $('input[name="'+ this.name +'"][value="basic"]').prop("checked"); if( this.value == "advanced" && bt) { //do stuff } else { $(this).prop('checked', false); } });

javascript jquery checkbox

No comments:

Post a Comment