通过消除最少用户,如果小于Max,则除以其他值

时间:2015-10-02 06:58:54

标签: javascript jquery dom jquery-selectors

我试图通过在每个条件中使用user class的值作为value_votes类的id并且将该值除以剩余的其他用户,尝试找到value_votes类的值小于所有用户的最少用户。通过消除每个用户达到最大值然后使用公式是最小用户的value_votes除以其他条件中的剩余用户数

localStorage.setItem('max_value','6');
    $(".users").each(function () {
        var users_id = $(this).val();
    var User_Vote_count = $('#'+users_id+'').val();

    var max_value = localStorage.getItem('max_value');

    if (User_Vote_count > max_value) {

        alert(users_id);
    } else if(User_Vote_count == max_value) {
             alert(users_id);
    }
});

HTML

<input type="text" value="A9AH98" class="users">
<input type="text" value="16B9BH" class="users">
<input type="text" value="CC9GHF" class="users">
<input type="text" value="A9D9G5" class="users">

<input type="text" class="value_votes" id="A9AH98" value="3">
<input type="text" class="value_votes" id="16B9BH" value="1">
<input type="text" class="value_votes" id="CC9GHF" value="4">
<input type="text" class="value_votes" id="A9D9G5" value="5">

1 个答案:

答案 0 :(得分:1)

正如在jsfiddle中所讨论的,我在这里发布代码:

localStorage.setItem('max_value','6');

var minValue;
var minItem;

//searching for minimum value among value inputs
$(".value").each(function() {
    var val = parseInt($(this).val(), 10);
    if (val && (!minItem || val < minValue)) {
        minItem = this;
        minValue = val;
    }
});

var minValId = $(minItem).attr("id");
console.log(minValId);

//searching for .user textboxes where value is not equal
var remainingUserElements = $("input.users:not([value='"+minValId+"'])");
console.log(remainingValueElements);

//searching for .value textboxes where id is not equal
var remainingValueElements = $("input.value:not([id='"+minValId+"'])");
console.log(remainingUserElements);

var maxValue = parseInt(localStorage.getItem('max_value'), 10);
console.log(maxValue);

//searching for .value elements where id is >= maxValue  
var geValueElements = remainingValueElements.filter(function(){
    return parseInt($(this).val(), 10) >= maxValue;
});

geValueElements.each(function(){
    //printing all values that are greater or equal to maxValue
    console.log($(this).val());
});