I am trying to make sure that all values, except one, is used in my drop downs - but there is an error, where its possible to cheat on it. And I am not sure why.
You can try it on: http://jsfiddle.net/BSxXk/2/ and use the values: 4 2 3 4
- This should produce an error since 1
is not used. I am using Firefox on Windows.
HTML:
<select name="answer" class="question-answer">
<option value="-1">Choose value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="answer" class="question-answer">
<option value="-1">Choose value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="answer" class="question-answer">
<option value="-1">Choose value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="answer" class="question-answer">
<option value="-1">Choose value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="result">d</div>
<button id="submit">Test</button>
JQuery:
var valid_values = [1, 2, 3, 4];
$("#submit").on("click", function(event) {
event.preventDefault();
var values = $.unique($('select.question-answer').map(function() {
var value = $(this).val();
if (value === "-1") {
return;
}
return value;
}));
if(values.length === valid_values.length) {
$("#result").html("Valid");
} else {
$("#result").html("All values (1-4) should be used in answering the question.");
}
});
Screenshot of error. 1 is not used, but says valid:
Please note this is a follow-up of my previous question: Make sure all values are used in drop downs