Multiple choice, partial randomize, select up to 3, exclude

Plus d'informations
il y a 2 mois 2 semaines - il y a 2 mois 2 semaines #169460 par stephanied
stephanied a créé le sujet : Multiple choice, partial randomize, select up to 3, exclude
Here's what I have for a multiple choice question type:
Code that allows me to select up to 3 (or any number) and disable selection of other answers.
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Maximun answers
		var maxAnswers = 3;
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		function checkMax() {
			$('input.checkbox', thisQuestion).prop('disabled', false);
			if($('input.checkbox:checked', thisQuestion).length >= maxAnswers) {
				$('input.checkbox', thisQuestion).not(':checked').prop('disabled', true);
			}
		}
 
		// Initial checkbox states
		checkMax();
 
		// Listener on the checkboxes
		$('input.checkbox', thisQuestion).change(function(e) {
			checkMax();
		});
 
		// Remove any "disabled" properties before submitting
		$('#movenextbtn, #movesubmitbtn').bind('click', function () {			
			$('input.checkbox', thisQuestion).prop('disabled', false);
		});
 
    });
</script>

I also have a code that will uncheck and disable selection of answer options if the exclusive answer is selected.
<script type="text/javascript" charset="utf-8">		
	$(document).ready(function(){
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Uncheck all excluded items
		$('div.question-item:last input.checkbox', thisQuestion).on('change', function(e) {
			if($(this).is(':checked')) {
				$('input.checkbox', thisQuestion).not($(this)).each(function(i) {
					$(this).prop('checked', false);
					$(this).nextAll('input:hidden:eq(0)').attr('value', '');
				});
			}
		});
	});
</script>

Additionally, I have code that allows me to fix answers to the bottom of the list:
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
		keepPos('{SGQ}',['6|6','7|7']) //	keepPos('{SGQ}',['code|position','code|position'])
	});
</script>

I frequently use the partial randomization code along with the other codes, however this time I'm attempting to combine all three of them. No matter which way I arrange the codes, or combine them, it doesn't work properly. I believe the issue lies within the "// Remove any "disabled" properties before submitting" section. When the exclusive option is selected, it allows for one more answer to be selected before the answer options are disabled. I'm hoping that a programmer can help me solve this issue. I've attached a .lss as well.
Pièces jointes :
Dernière édition: il y a 2 mois 2 semaines par stephanied.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 mois 2 semaines #169462 par Joffm
Joffm a répondu au sujet : Multiple choice, partial randomize, select up to 3, exclude
Hi,
I assume you do not work with version 3.x
In your scripts there is "#movenextbtn".

Well, I used this in version 2.73 - and the "normal settings: max answers, exclusive option.
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
 
		// Identify this question
		var qID = {QID};
		var thisQuestion = $('#question'+qID);
 
		// Define the sub-question codes to be placed last
		var lastItems = [ 'SQ005','SQ008'];
 
		// Loop through those sub-question codes 
		$.each(lastItems, function(i, val) {
			// Move that item to the end of the list
			// Multi-choice question
			if($(thisQuestion).hasClass('multiple-opt')) {
				$('.question-item[id$=X'+qID+val+']', thisQuestion).parent().appendTo($('.subquestion-list', thisQuestion));
			}
			// Array question
			if($(thisQuestion).hasClass('array-flexible-row')) {
				$('.answers-list[id$=X'+qID+val+']', thisQuestion).appendTo($('table.subquestion-list', thisQuestion));
			}
		});	
	});
</script> 
<script type="text/javascript" charset="utf-8">		
	$(document).ready(function(){
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Uncheck all excluded items
		$('div.question-item:last input.checkbox', thisQuestion).on('change', function(e) {
			if($(this).is(':checked')) {
				$('input.checkbox', thisQuestion).not($(this)).each(function(i) {
					$(this).prop('checked', false);
					$(this).nextAll('input:hidden:eq(0)').attr('value', '');
				});
			}
		});
	});
</script>

See the effect:




Best regards
Joffm

BTW: In your last script there is a function "keepPos", but it is not defined.


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Pièces jointes :

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 mois 2 semaines #169481 par stephanied
stephanied a répondu au sujet : Multiple choice, partial randomize, select up to 3, exclude
Yes, I am using version 2.73. The keepPos function is defined within my css template.

I appreciate your effort to solve this issue. However, I'm still wishing for the code to disable answer options once the max (3) answers have been selected AND disables answers when the exclusive option is selected. The codes I provided above successfully function this way independently but do not work properly when combined.

Connexion ou Créer un compte pour participer à la conversation.

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant

Inscrivez-vous à notre Newsletter!