Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Search Results (Searched for: exclusive-item)

  • percyherrera
  • percyherrera's Avatar
04 Apr 2025 01:36
Ayúdenos a ayudarle y rellene los siguientes campos:.
Su versión de LimeSurvey: 6.12.3
Servidor propio o LimeSurvey Cloud: LimeSurvey Cloud
Plantilla de diseño utilizada: vanilla
==================
Amigos, comparto el siguiente script que utilizo para el siguiente escenario: Imaginemos que tenemos una pregunta de tipo respuesta multiple con varios items en una columna y en el ultimo campo tenemos la opción "Ninguno" o "N/A". La idea es que si selecciono el item "Ninguno" se desmarquen los otros items y si selecciono cualquiera de los items validos se desmarque la opción "Ninguno" si estuviera seleccionada.  Esto lo he podido lograr con el siguiente script y lo comparto para usarlo en un futuro.
Espero sea de ayuda:
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
    // Call the exclude function using question ID
    excludeOpt({QID});
  });
 
  // A function to make the last option in a single-column multiple choice exclusive
  function excludeOpt(qID) {
    var thisQuestion = $('#question' + qID);
 
    // Add classes to the checkbox items
    $('input[type="checkbox"]', thisQuestion).each(function(i) {
      $(this).closest('li').addClass('normal-item');
    });
 
    // Mark the last checkbox as exclusive
    $('li.normal-item:last', thisQuestion).removeClass('normal-item').addClass('exclusive-item');
 
    // A listener on the checkboxes
    $('input[type="checkbox"]', thisQuestion).on('change', function(event) {
      handleExclusive($(this).closest('li'));
    });
 
    function handleExclusive(thisItem) {
      // Uncheck the appropriate boxes
      if ($(thisItem).hasClass('normal-item')) {
        $('.exclusive-item input[type="checkbox"]', thisQuestion).prop('checked', false);
      } else {
        $('.normal-item input[type="checkbox"]', thisQuestion).prop('checked', false);
      }
 
      // Check conditions (relevance)
      $('li.checkbox-item', thisQuestion).each(function(i) {
        var thisValue = '';
        if ($('input[type="checkbox"]', this).is(':checked')) {
          thisValue = 1;
        }
        var thisSGQA = $('input[type="checkbox"]', this).attr('id').replace(/cbox_/, '');
 
        $('input[type="hidden"]', this).attr('value', thisValue);
        fixnum_checkconditions(thisValue, thisSGQA, 'hidden');
      });
    }
  }
</script>
  • tpartner
  • tpartner's Avatar
27 Nov 2024 18:07
Replied by tpartner on topic Need Help with Dual Matrix Array Filter
In version 5.x, add an "N/A" answer to scale-2 and insert this script:

Code:
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).on('ready pjax:scriptcomplete',function() {
 
        // Identify this question
        var thisQuestion = $('#question{QID}');
 
        // Identify the "exclusive" column(s)
        // Multiple columns separated by commas
        var exclusiveCols = [1,2];
 
        // Assign classes to various elements
        $('tr[id^="javatbd"]', thisQuestion).each(function(i){
            var column = 1;
            var scale = 1;
            $('td', this).each(function(i) {
                if($(this).hasClass('answer-item')) {
                    $(this).addClass('scale-'+scale+' column-'+column+'');
                    column++;
                }
                else if(scale == 1 &amp;&amp; $(this).hasClass('dual_scale_separator')) {
                    column = 1;
                    scale = 2;
                }
                else if(scale == 2 &amp;&amp; $(this).hasClass('dual_scale_separator')) {
                    column = 1;
                    scale = 3;
                }
            });
            $('td.scale-2:last', this).addClass('na-item');
        });
        $(exclusiveCols).each(function(i) {
          $('td.scale-1.column-'+this, thisQuestion).addClass('exclusive-item');
        });
 
        // Hide the "N/A" column
        var naIndex = $('td.na-item:eq(0)', thisQuestion).index();
        $('td.na-item', thisQuestion).hide();
        $('.questions-list .dsheader:last', thisQuestion).attr('colspan', Number($('.questions-list thead tr.groups .dsheader:last', thisQuestion).attr('colspan'))-1);
        $('.questions-list thead tr:not(.groups) th.answer-text:not(.noanswer-text):last', thisQuestion).hide();
        $('colgroup.group-2 col:last', thisQuestion).hide();
 
        // Listener on the radios
        $('td.scale-1 input:radio', thisQuestion).on('click', function(e) {
            var thisCell = $(this).closest('td');
            var thisRow = thisCell.closest('tr');
            var thisNA = $('.na-item input:radio', thisRow);
            var naValue = '';
            if(thisCell.hasClass('exclusive-item')) {
                naValue = thisNA.val();
                thisNA.trigger('click');
                $('td.scale-2:not(.na-item) input:radio', thisRow).prop('disabled', true);
            }
            else {
                thisNA.prop('checked', false);
                $('td.scale-2:not(.na-item) input:radio', thisRow).prop('disabled', false);
            }
            // Fire ExpressionScript
            naName = thisNA.attr("name");
            naName = naName.replace('#', '_');
            $("#java" + naName).val(naValue);
            ExprMgr_process_relevance_and_tailoring('change', naValue, 'radio');
        });
 
        // Initial states
        $('td.exclusive-item input:radio:checked', thisQuestion).each(function(i) {
            var thisRow = $(this).closest('tr');
            console.log(thisRow);
            $('.na-item input:radio', thisRow).trigger('click');
            $('td.scale-2:not(.na-item) input:radio', thisRow).prop('disabled', true);
        });
 
    });
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...8547.lss
File Size:41 KB
  • tpartner
  • tpartner's Avatar
07 Nov 2024 14:34 - 07 Nov 2024 14:37
This script, placed in the question text, will render the last two columns exclusive.
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">
    $(document).on('ready pjax:scriptcomplete',function(){
 
        var thisQuestion = $('#question{QID}')
 
        // Add some classes to the checkbox cells
        $('tr[id^="javatbd"]', thisQuestion).each(function(i) {
            // Last two columns exclusive
            $('td.checkbox-item', this).slice(-2).addClass('exclusive-item');
        });
 
        // A function to un-check boxes
        function resetCheckbox(thisItem) {
            $(':hidden', thisItem).val('');
            $(':checkbox', thisItem).prop('checked', false).trigger('change');
        }
 
        // A listener on the checkboxes
        $(':checkbox', thisQuestion).on('change', function(e) {
            if($(this).is(':checked')) {
                var thisItem = $(this).closest('.answer-item');                
                var thisRow = $(this).closest('tr');
                var items = $('td.answer-item.exclusive-item', thisRow);
                if($(thisItem).hasClass('exclusive-item')) {
                    items = $('td.answer-item', thisRow).not(thisItem);
                }
                $.each(items, function(i, el) {
                    resetCheckbox(el);
                });
            }
        });
    });
</script>
Displaying 1 - 3 out of 3 results.

Lime-years ahead

Online-surveys for every purse and purpose