- Posts: 73
- Thank you received: 1
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclusiveArrayRows function with question and sub-question IDs var sqIDs = ['SQ6', 'SQ7'] exclusiveArrayRows({QID}, sqIDs); }); // A function to render array rows mutually exclusive // Parameters: //// 1) The question ID //// 2) An array of sub-question IDs to be exclusive function exclusiveArrayRows(qID, sqIDs) { // Identify the question var thisQuestion = $('#question'+qID); // Loop through the sub-question IDs $(sqIDs).each(function(i, val) { // Assign a class $('tr.answers-list[id$="X'+qID+val+'"]', thisQuestion).addClass('exclusive-row'); }); // Initial radio states $('.exclusive-row input.radio:checked', thisQuestion).each(function(i) { // Identify rows and values var thisValue = $(this).val(); var thisRow = $(this).closest('tr.answers-list'); var otherRows = $('tr.answers-list.exclusive-row', thisQuestion).not(thisRow); // Disable the appropriate radio(s) in the other rows(s) $('input.radio[value="'+thisValue+'"]', otherRows).prop('disabled', true); }); // Listener on the exclusive radios $('.exclusive-row input.radio', thisQuestion).on('click', function(e) { // Identify rows and values var thisValue = $(this).val(); var thisRow = $(this).closest('tr.answers-list'); var otherRows = $('tr.answers-list.exclusive-row', thisQuestion).not(thisRow); // Reset the other rows(s) $('input.radio', otherRows).prop('disabled', false); // Disable the appropriate radio(s) in the other rows(s) $('input.radio[value="'+thisValue+'"]', otherRows).prop('disabled', true); }); } </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclusiveArrayRows function with question and sub-question IDs var sqIDs = ['SQ6', 'SQ7'] exclusiveArrayRows2({QID}, sqIDs); }); // A function to conditionally disable array answers // Parameters: // 1) The question ID // 2) An array of sub-question IDs to be exclusive function exclusiveArrayRows2(qID, sqIDs) { // Identify the question var thisQuestion = $('#question'+qID); // Loop through the sub-question IDs $(sqIDs).each(function(i, val) { // Assign a class $('tr.answers-list[id$="X'+qID+val+'"]', thisQuestion).addClass('conditional-row'); }); // Initial radio states $('.conditional-row input.radio:checked', thisQuestion).each(function(i) { // Call a function to handle the radios handleRadios($(this), $(this).val()); }); // Listener on the exclusive radios $('.conditional-row input.radio', thisQuestion).on('click', function(e) { // Call a function to handle the radios handleRadios($(this), $(this).val(), true); }); // A function to disable/enable radios function handleRadios(checkedRadio, thisValue, onClick) { var thisRow = $(checkedRadio).closest('tr.answers-list'); var otherRows = $('tr.answers-list.conditional-row', thisQuestion).not(thisRow); if(onClick !== undefined) { // Reset the other rows on click $('input.radio', otherRows).prop('disabled', false); } if(thisValue != 3) { //Determine which radios to disable if(thisValue < 3) { var disabledInputs = $('input.radio', otherRows).filter(function(e) { return $(this).val() < 3; }); } else { var disabledInputs = $('input.radio', otherRows).filter(function(e) { return $(this).val() > 3; }); } // Disable the appropriate radios in the other rowss $(disabledInputs).prop('checked', false).prop('disabled', true); } } } </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclusiveArrayRows function with question and sub-question IDs var sqIDs = ['SQ6', 'SQ7'] exclusiveArrayRows2({QID}, sqIDs); }); // A function to conditionally disable array answers // Parameters: // 1) The question ID // 2) An array of sub-question IDs to be exclusive function exclusiveArrayRows2(qID, sqIDs) { // Identify the question var thisQuestion = $('#question'+qID); // Loop through the sub-question IDs $(sqIDs).each(function(i, val) { // Assign a class $('tr.answers-list[id$="X'+qID+val+'"]', thisQuestion).addClass('conditional-row'); }); // Initial radio states $('.conditional-row input.radio:checked', thisQuestion).each(function(i) { // Call a function to handle the radios handleRadios($(this), $(this).val()); }); // Listener on the exclusive radios $('.conditional-row input.radio', thisQuestion).on('click', function(e) { // Call a function to handle the radios handleRadios($(this), $(this).val(), true); }); // A function to disable/enable radios function handleRadios(checkedRadio, thisValue, onClick) { var thisRow = $(checkedRadio).closest('tr.answers-list'); var otherRows = $('tr.answers-list.conditional-row', thisQuestion).not(thisRow); if(onClick !== undefined) { // Reset the other rows on click $('input.radio', otherRows).prop('disabled', false); $('input.radio[value="6"]', otherRows).prop('checked', false); } if(thisValue != 3) { //Determine which radios to disable if(thisValue == 6) { $('input.radio[value="'+thisValue+'"]', otherRows).prop('checked', true); $('input.radio', thisRow).prop('disabled', false); } else if(thisValue < 3) { var disabledInputs = $('input.radio', otherRows).filter(function(e) { return $(this).val() < 3; }); // Disable the appropriate radios in the other rows $(disabledInputs).prop('checked', false).prop('disabled', true); } else { var disabledInputs = $('input.radio', otherRows).filter(function(e) { return $(this).val() > 3 && $(this).val() != 6; }); // Disable the appropriate radios in the other rows $(disabledInputs).prop('checked', false).prop('disabled', true); } } else { //$('input.radio', thisRow).prop('disabled', false); } } // Listener on the radio cells $('.conditional-row .answer-item', thisQuestion).on('click', function(e) { if($('input.radio', this).is(':disabled')) { return false; } }); } </script>