最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery count selection-boxes that have an item selected - Stack Overflow

programmeradmin4浏览0评论

i wonder if there is a more efficient way of counting the selections of a user:

This is the selection dropdown that is multiple times on the page:

<select class="span2 jquery-countable" name="category[7]">
    <option></option>
    <option value="1">1. vote</option>
    <option value="2">2. vote</option>
    <option value="3">3. vote</option>
    <option value="4">4. vote</option>
</select>

This is my js that counts the selections:

$(document).ready(function () {

    $('#selected-count').html('You choose ' + getCount() + ' entries');

    function getCount() {
        prio1 = $('.jquery-countable option:selected[value="1"]').length;
        prio2 = $('.jquery-countable option:selected[value="2"]').length;
        prio3 = $('.jquery-countable option:selected[value="3"]').length;
        prio4 = $('.jquery-countable option:selected[value="4"]').length;

        return prio1 + prio2 + prio3 + prio4;
    }

    $('.jquery-countable').change(function () {
        $('.jquery-countable option:selected').each(function () {
            $('#selected-count').html('You choose ' + getCount() + ' entries');
        })
    })
});

My goal is it to count all selections that where made by the user that are not empty?!

Is there a more efficient way?

Thanks!

i wonder if there is a more efficient way of counting the selections of a user:

This is the selection dropdown that is multiple times on the page:

<select class="span2 jquery-countable" name="category[7]">
    <option></option>
    <option value="1">1. vote</option>
    <option value="2">2. vote</option>
    <option value="3">3. vote</option>
    <option value="4">4. vote</option>
</select>

This is my js that counts the selections:

$(document).ready(function () {

    $('#selected-count').html('You choose ' + getCount() + ' entries');

    function getCount() {
        prio1 = $('.jquery-countable option:selected[value="1"]').length;
        prio2 = $('.jquery-countable option:selected[value="2"]').length;
        prio3 = $('.jquery-countable option:selected[value="3"]').length;
        prio4 = $('.jquery-countable option:selected[value="4"]').length;

        return prio1 + prio2 + prio3 + prio4;
    }

    $('.jquery-countable').change(function () {
        $('.jquery-countable option:selected').each(function () {
            $('#selected-count').html('You choose ' + getCount() + ' entries');
        })
    })
});

My goal is it to count all selections that where made by the user that are not empty?!

Is there a more efficient way?

Thanks!

Share Improve this question asked May 30, 2012 at 12:42 FMKFMK 1,1401 gold badge14 silver badges29 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 7

This will return the number of selected items that is not empty as you wished:

function getCount(){
   return $("select.jquery-countable option:selected[value!='']").length;
}

here is a way to do it

$(document).ready(function () {

    $('#selected-count').html('You choose ' +0 + ' entries');



    $('.jquery-countable').change(function () {
        var count = 0;
        $('.jquery-countable').each(function () {
            if($.trim($(this).val()).length > 0)
                count++;
        });
        $('#selected-count').html('You choose ' +count + ' entries');
    })
});​

Working Fiddle

Try something like this

$("select").change(function(){           
   var count = $("select.jquery-countable option:selected[value!='']").length;
   console.log(count);
});

Yeah, just take the [value=#] off. The more generic selector will return an array of selected values, which should contain just 1 in the case of a select.

function getCount() {
    return $('.jquery-countable option:selected').length;
}

This will count the total number of "selected" options on the page that are in a select with the class jquery-countable.

发布评论

评论列表(0)

  1. 暂无评论