确保使用所有值 [英] Ensure all values are used

查看:93
本文介绍了确保使用所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力确保除了一个之外的所有值都用在我的下拉中 - 但是有一个错误,它可能会欺骗它。而且我不确定原因。



你可以尝试一下:

解决方案

欺骗问题,小提琴中的代码与发布的代码不匹配。小提琴适用于发布的代码



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;
}
返回值;
}));
if(values.length === valid_values.length){
$(#result ).html(有效);
}其他{
$(#result)。html(所有值(1-4)应该用于回答问题。);
}
});

UPDATE



使用jquery 1.10.1适用于我 http://jsfiddle.net/BSxXk/5/
使用上述任何内容,对我来说不起作用



我仍然认为此处使用unique是不正确的,因为$ .unique仅适用于DOM元素 https://api.jquery.com/jQuery.unique/ 问题答案中的地图功能只返回数字,所以它很完美感觉唯一不起作用


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

解决方案

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

这篇关于确保使用所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆