1

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:

enter image description here

Please note this is a follow-up of my previous question: Make sure all values are used in drop downs

Community
  • 1
  • 1
janhartmann
  • 14,713
  • 15
  • 82
  • 138

2 Answers2

0

Trick question, code in the fiddle does not match code posted. The fiddle works fine with the posted code

http://jsfiddle.net/BSxXk/2/

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.");
    }
});

UPDATE

using jquery 1.10.1 works for me http://jsfiddle.net/BSxXk/5/ using anything above, does not work for me

I still think the use of unique is incorrect here, because $.unique only works on DOM elements https://api.jquery.com/jQuery.unique/ the map function on the question-answer only returns numbers so it makes perfect sense that unique does not work

Huangism
  • 16,278
  • 7
  • 48
  • 74
0

Try this: http://jsbin.com/ciwaq/1/edit

$('button').click(function(){

  var values = ['1','2','3','4'];
  var selectValues = [];

  $('select').each(function(){
    selectValues.push($(this).val());
  });

  if($(values).not($(values).not(selectValues).get()).get().length !== values.length) {
     $('#result').html('All values are not used');
  }
  else {
    $('#result').html('All values ARE USED :)');
  }

});
Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42