Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:

THEMA: Conditions not working

Conditions not working 10 Monate 3 Tage her #100167

  • w0928
  • w0928s Avatar
  • OFFLINE
  • Junior Lime
  • Beiträge: 27
  • Dank erhalten: 3
  • Karma: 1
I have an array(numbers) checkbox style question with 4 columns. The first 2 columns are mutually exclusive from all the others... so the user can only check column 1 by itself, column 2 by itself, column 3 with or without 4, and column 4 with or without 3.

I have the code below that implements this behavior.

In addition to this question I have another question with a condition on it that says to only show that question when the user has checked columns 2, 3, or 4 for one of the rows in my array(numbers) checkbox style question. I know the condition is set properly b/c if I remove the code below it works.

If a user checks a checkbox in columns 2, 3, or 4 then condition works and the question is shown. However, if the user checks on column 1, which triggers the code to uncheck columns 2, 3, and 4... the subsequent question still appears. It doesn't go away like it should.

So basically I am asking why the code below is causing this and how can I go about fixing it? I'm guessing I need to call .trigger('click') to uncheck those columns, but then I get stuck in a loop. Sorry if this is more of a javascript question. I'm just trying to figure out how this all works and would really, really appreciate any help you all could provide.

Thanks.
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
		// Call the exclude function using question ID
		excludeOpt ({QID});
	});
 
	// A function to make the first option in each array row exclusive
	function excludeOpt (qID) {
 
		var thisQuestion = $('#question'+qID)
 
		// Add some classes to the checkbox cells
		$('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt1');
		$('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt2');
		$('table.question tbody tr', thisQuestion).each(function(i) {
			$('.answer_cell_001:first', this).removeClass('nonExclusiveOpt1').addClass('exclusiveOpt1');
			$('.answer_cell_002:first', this).removeClass('nonExclusiveOpt2').addClass('exclusiveOpt2');
		});
 
		// A listener on the checkbox cells
		$('table.question tbody td', thisQuestion).click(function (event) {
 
			// Set some vars
			var thisRow = $(this).closest('tr');
 
			// Uncheck the appropriate boxes in a row
			if ($(this).hasClass('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}				
			else if ($(this).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(this).hasClass('nonExclusiveOpt1') && $(this).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
		});
 
		// A listener on the checkboxes
		$('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) {
 
			// Set some vars
			var thisRow = $(this).closest('tr');
			var thisCell = $(this).closest('td');
 
			// Uncheck the appropriate boxes in a row
			if ($(thisCell).hasClass('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('nonExclusiveOpt1') && $(thisCell).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
		});
	}	
</script>
Letzte Änderung: 10 Monate 3 Tage her von w0928.
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Conditions not working 10 Monate 2 Tage her #100190

  • tpartner
  • tpartners Avatar
  • OFFLINE
  • LimeSurvey Team
  • Beiträge: 4069
  • Dank erhalten: 748
  • Karma: 341
Ah, we'll have to fire the fixnum_checkconditions() function for all affected checkboxes.

Try this:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
		// Call the exclude function using question ID
		excludeOpt ({QID});
	});
 
	// A function to make the first option in each array row exclusive
	function excludeOpt (qID) {
 
		var thisQuestion = $('#question'+qID)
 
		// Add some classes to the checkbox cells
		$('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt1');
		$('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt2');
		$('table.question tbody tr', thisQuestion).each(function(i) {
			$('.answer_cell_001:first', this).removeClass('nonExclusiveOpt1').addClass('exclusiveOpt1');
			$('.answer_cell_002:first', this).removeClass('nonExclusiveOpt2').addClass('exclusiveOpt2');
		});
 
		// A listener on the checkbox cells
		$('table.question tbody td', thisQuestion).click(function (event) {
 
			// Set some vars
			var thisRow = $(this).closest('tr');
 
			// Uncheck the appropriate boxes in a row
			if ($(this).hasClass('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}				
			else if ($(this).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(this).hasClass('nonExclusiveOpt1') && $(this).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
 
			checkRowConditions(thisRow);
		});
 
		// A listener on the checkboxes
		$('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) {
 
			// Set some vars
			var thisRow = $(this).closest('tr');
			var thisCell = $(this).closest('td');
 
			// Uncheck the appropriate boxes in a row
			if ($(thisCell).hasClass('exclusiveOpt1')) {
				$('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('exclusiveOpt2')) {
				$('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
			else if ($(thisCell).hasClass('nonExclusiveOpt1') && $(thisCell).hasClass('nonExclusiveOpt2')) {
				$('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
				$('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
			}
 
			checkRowConditions(thisRow);
		});
 
		// A function to check conditions for all checkboxes in a row
		function checkRowConditions(thisRow) {
			$('input[type="checkbox"]', thisRow).each(function(i) {
				var thisAnsID = $(this).attr('id').replace(/cbox_/, '');
				var thisAnswerInput = document.getElementById('answer'+thisAnsID);
				var thisJavaInput = document.getElementById('java'+thisAnsID);
				var thisAnsValue = 0;
				if(this.checked) {
					thisAnsValue = 1; 
				} 
				thisAnswerInput.value = thisAnsValue;
				thisJavaInput.value = thisAnsValue;
				fixnum_checkconditions(thisAnsValue, thisAnsID, thisAnswerInput.type);  
				return true;
			});
		}
	}	
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Folgende Benutzer bedankten sich: w0928

Conditions not working 10 Monate 2 Tage her #100192

  • w0928
  • w0928s Avatar
  • OFFLINE
  • Junior Lime
  • Beiträge: 27
  • Dank erhalten: 3
  • Karma: 1
Thank you! I would have never figured this out on my own.
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Moderatoren: ITEd
Ladezeit der Seite: 0.213 Sekunden
Donation Image