• Das Erstellen neuer Accounts wurde ausgesetzt. Bei berechtigtem Interesse bitte Kontaktaufnahme über die üblichen Wege. Beste Grüße der Admin

[FRAGE] Script zur Realisierung eines Tests mit verschiedenen Fragen

dbarthel

Lounge-Member
Ich habe von einer US-Seite ein Script, welches aus XML-Files einen Test baut.
Dabei ist im Test die erste Frage, ob man in den USA wohnt.

Im Script gibt es einen Bereich:
Code:
//show this if they answered no to first question
 if(flagged){$('#results div').prepend("<p class='flagged'>You must be a US Citizen to gain employment.</p>")};

der dafür sorgt, das wenn jemand die erste Frage mit NEIN beantwortet, eine entsprechende Meldung ausgegeben wird.


Nun möchte ich das Script so abändern, das man auch die erste Frage mit NEIN beantworten kann, ohne das eine fest definierte Meldung ausgegeben wird.


Habe schon alles probiert, aber ich finde nicht heraus, wo und wie man das Script ändern müsste, damit auch die erste Frage mit Nein beantwortet werden kann.
Vielleicht kann jemand von euch helfen.

Hier mal das Script komplett:
Code:
$(document).ready(function() {
	//lets load our questions
	$.ajax({
			type: "GET",
			url: "../xml/questions_answers.xml",
			dataType: "xml",
			success: function(xml){
				var i = 1;
				var j = 1;
				var num = 1;
				var p = false;
				$(xml).find('question').each(function(){
					var question = $(this).find("text").text();
					var category = $(this).find("category").text();
					var note = $(this).find("note").text();
					var type = $(this).find("type").text();
					var answer = [];
						answers = $(this).find('answer').each(function(){
							answer.push($(this).find("answer").text());
						});
					if (p == false && category == 'p'){num = 3; p = true;}
				//build html structure				
						var container = $('<div></div>')
							.hide()
							.addClass('question')
							.addClass(category)
							.attr('id', 'question-container'+i);
						var myhtml = $('<fieldset></fieldset>')
							.append($('<legend><span>Question #'+num+'</span><div>'+question+'</div></legend>')
								.attr({ 'class' : 'question--'+i })
							)
							answers.each(function(){
								myhtml.append($('<input type="'+type+'"/>')
									.attr({ 'id' : 'question--'+i+j, 'name' : i, 'value': $(this).text() })
									.click(function(){
										var boxes = $(this).parent().find(":input:checked");
										var allboxes = $(this).parent().find(":input");
										$(boxes).each(function(){
											//do stuff here with this
											if($(boxes).size() > 1 && $(this).val() == "None"){
												//$('.error').append("<p>you selected none and another option, please uncheck everything to select none, or visa-versa</p>");
												$('.error').show();
												$('label[for="'+$(this).attr('id')+'"]').css({border : "1px solid red"});
											} else {
												$('.error').hide();
												$(allboxes).each(function(){
													$('label[for="'+$(this).attr('id')+'"]').css({border : "0px solid red"});
												});
											}
										});
									})											
								)
							.append($('<label>'+$(this).text()+'</label>')
												.attr({ 'for' : 'question--'+i+j })
											)
								j++;
							});
							if (note){
								myhtml.append('<p class="small">'+note+'</p>');
							}
							container.append(myhtml);
							
							$('.questions').append(container);
				i++;
				num++;
				});

//next and pre btns	
				 var prev = $('<a href="#" class="prev">Previous</a>');
				 var next = $('<a href="#" class="next">Next</a>');
				 next.click(function(e){
					e.preventDefault();
					$('.wrong').hide();
					var proceed = true;
				 //check for selection
					if($(this).parent().parent().find(':input').is(':checked') || $(this).parent().parent().find('select[id*=right]').find('option').length > 0){
					//check for none
					var boxes = $(this).parent().parent().find(":input:checked");
						$(boxes).each(function(){
							if($(boxes).size() > 1 && $(this).val() == "None"){
								$('.error').show();
								$('label[for="'+$(this).attr('id')+'"]').css({border : "1px solid red"});
								proceed = false;
							}
						});
						if(proceed == false){
							return false;
						}
						
						//now check to see if we should be in student or pro
						if ($(this).parent().parent().find('.question--2').length > 0){
							var checked = $(this).parent().parent().find(':input').index($(this).parent().parent().find('input:checked'));
							if (checked == 4){
								$('.p').first().fadeIn(500);
								$(this).parent().parent().hide();
							} else {
								$(this).parent().parent().next().fadeIn(500);
								$(this).parent().parent().hide();
							}
						} else {
						//check here if at end show results or keep going
							var slast = $(this).parent().parent().index($('.s').last());
							var plast = $(this).parent().parent().index($('.p').last());
							if(slast == 0 || plast == 0){
								//get results
								$('.question').hide();
								$('.questions').hide();
								$('#results').fadeIn(500);
								getResults();
							} else {
								$(this).parent().parent().next().fadeIn(500);
								$(this).parent().parent().hide();
							}
						}
					} else {
						$('.wrong').fadeIn(500);
						return false;
					}
				 	//scroll to top
					scrollContentIntoView();
				 });
				 prev.click(function(e){
					e.preventDefault();
					$('.wrong').hide();
					$(this).parent().parent().find(':input').attr('checked', false);
					var ps = $(this).parent().parent().index($('.p').first());
					if (ps == 0){
						$('.g').last().fadeIn(500);
						$(this).parent().parent().hide();
					} else {
						$(this).parent().parent().prev().fadeIn(500);
						$(this).parent().parent().hide();
					}
				 	//scroll to top
					scrollContentIntoView();
				 });
				 var btns = $('<div class="btns"></div');
				 var pprbtn = $('<div class="pprbtn"></div');
				 $(btns).append(prev);
				 $(btns).append(next);
				 $('.question').append(pprbtn);
				 $('.question').append(btns);
				 var startover = $('<a href="#" class="startover">Start Over</a>');
					 startover.click(function(e){
						 e.preventDefault();
						 location.reload();
					 });
				 $('.question').append(startover);
				 $('.questions').append("<span class='bottom'></span>");
					  },
					error: function() {
						//change this to consol to log
						alert("An error occurred while processing XML file.");
				  }
		//end ajax calls
	  });
	//init btns for results, error, and start
	 $('#start').click(function(e){
		e.preventDefault();
		$('.start').hide();
		$('#question-container1').show();
	 });
	 
	 $('.startover').click(function(e){
		e.preventDefault();
		 location.reload();
	 });
	 
	 $('.closebtn a').click(function(e){
		e.preventDefault();
		$('.wrong').fadeOut(500);
		$('.error').fadeOut(500);
	 });
	 $('.questions').click(function(){
		$('.wrong').fadeOut(500);
	 });
});
//end on dom read functions
//parse all inputs and query xml job feed.
function getResults(){
	var allRadios = $(":input:checked");
	var allSelects = $('select[id*=right]');
	//use name - 1 for index in array to check
	var questions = [];
	var questionsAnswers = [];
	var jobs = [];
	var myjobs = [];
	$.ajax({
		type: "GET",
		url: "../xml/new_categories.xml",
		dataType: "xml",
		success: function(xml){
		var i = 1;
		var flagged = false;
			$(xml).find('node').each(function(){
				var question = $(this).find("text").text();
				questions.push(question);
				//var answer = [];
				var job = [];
				var link;
				var score;
					$(this).find('answer').each(function(){
						//answer.push($(this).find('val').text());
						var val = $(this).find('val').text();
						score = $(this).find('score').text();
						job[val] = [];
						$(this).find('job').each(function(){
							job[val].push($(this).text());
						});
					});
					
				var questionAnswers = {question :  $(this).find("text").text(), jobs: job, scores : score}
				questionsAnswers.push(questionAnswers);
			});
			//qa has question and answers with answers and jobs
			var qa = $.makeArray(questionsAnswers);
			$.each(allRadios, function(){
					var indy = $(this).attr('name');
					var answer = $(this).val();
					
					if (indy == 1 && answer == 'No'){flagged = true;}
					indy--;
				if (qa[indy].jobs[answer] !== undefined){
					var score = parseInt(qa[indy].scores);
					$.each(qa[indy].jobs[answer], function(i,v){
						//for each job check to see if it exists, if not create it, if exists then add score to it.
						//this array (myjobs) should have jobs[key] scores[value]
						var added = false;
						$.each(myjobs, function(indy, val){
							 if (v == val.job) {
							   var existing = parseInt(val.total);
							   var newscore = score + existing;
							   val.total = newscore;
							   added = true;
							 }
						});
						if (!added) {
							var jbs = {job: v, total: score};
							myjobs.push(jbs);
						}
					});
				}
			});
			
			$.each(allSelects, function(){
					var indy = $(this).attr('name');
					indy--;
					$(this).find('option').each(function(){
						var answer = $(this).val();
					if (qa[indy].jobs[answer] !== undefined){
						var score = parseInt(qa[indy].scores);
						$.each(qa[indy].jobs[answer], function(i,v){
							var added = false;
							$.each(myjobs, function(indy, val){
								 if (v == val.job) {
								   var existing = parseInt(val.total);
								   var newscore = score + existing;
								   val.total = newscore;
								   added = true;
								 }
							});
							if (!added) {
								var jbs = {job: v, total: score};
								myjobs.push(jbs);
							}
						}); 
					}
					});	
			});	
//get links
			
				$.ajax({
					type: "GET",
					url: "../xml/links.xml",
					dataType: "xml",
					success: function(xml){
						var links = [];
						$(xml).find('job').each(function(){
							var jb = $(this).find('title').text();
							links[jb] = []
							links[jb].push($(this).find('link').text());
						});
					//console.log(links);
					
					myjobs.sort(function(a, b) {
					   return (a.total < b.total) ? 1 : -1;
					});
					//show results
					// first get mean the cut above that line
					$('#results .jobs').html('');
					var array = [];
					$.each(myjobs, function(indy, val){
						array.push(parseInt(val.total));
					});	
					var mean = 0;
					$.each(array,function() {
						mean += parseInt(this);
					});
					tttl = array.length;
					mean = mean / tttl;
					var top_median = (mean + array[0]) / 2;
					var i = 0;
					
					//$('#results .jobs').prepend('top - '+top_median+' mean - '+mean);
					$.each(myjobs, function(indy, val){
					
					  var inn = parseInt(val.total);
					  if (inn > top_median){
						$('#results .jobs').append("<li><a href='"+links[val.job]+"'>"+val.job+"</a></li>");

					  }
					  i++;
					});
					//show this if they answered no to first question
					 if(flagged){$('#results div').prepend("<p class='flagged'>You must be a US Citizen to gain employment.</p>")};
							
							}
						});
					//end
		},
		  error: function() {
			//change this to consol to log
			console.log("An error occurred while processing XML file.");
		}
	});
}

function scrollContentIntoView() {
	var scrollToElem = $("#main").offset().top;
	$("html, body").animate({ scrollTop: scrollToElem }, "slow");
}
 
Die Zeile, die Du editieren musst, lautet:
Code:
if (indy == 1 && answer == 'No'){flagged = true;}

... würdest Du diese entnehmen, wäre "flagged" dauerhaft false und Dein US-Citizen käme nicht.
 
Zurück
Oben