jquery multiple checkboxes array

<input type="checkbox" name="options[]" value="1" />
<input type="checkbox" name="options[]" value="2" />
<input type="checkbox" name="options[]" value="3" />
<input type="checkbox" name="options[]" value="4" />
<input type="checkbox" name="options[]" value="5" />

Jak mogę utworzyć tablicę z zaznaczonymi wartościami pól wyboru?

UWAGA: (Ważne) potrzebuję tablicy jak [1, 2, 3, 4, 5] i nie jak ["1", "2", "3", "4", "5"] .
Uwaga: Istnieje około 50 pól wyboru.

Czy ktoś może mi pomóc? Proszę! Dziękuję!
Author: Dormouse, 2011-05-29

4 answers

var checked = []
$("input[name='options[]']:checked").each(function ()
{
    checked.push(parseInt($(this).val()));
});
 65
Author: Dormouse,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-29 08:44:53

Możesz użyć $.map() (lub nawet .map() Funkcja działająca na obiekcie jQuery), aby uzyskać tablicę sprawdzonych wartości. Operator unary ( + ) rzuci ciąg znaków na liczbę

var arr = $.map($('input:checkbox:checked'), function(e,i) {
    return +e.value;
});

console.log(arr);

Oto przykład

 57
Author: Russ Cam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-29 08:56:56
var checkedString = $('input:checkbox:checked.name').map(function() { return this.value; }).get().join();
 2
Author: user2503922,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-13 09:11:54

Jeśli masz klasę dla każdego z pól wejściowych, możesz to zrobić jako

        var checked = []
        $('input.Booking').each(function ()
        {
            checked.push($(this).val());
        });
 2
Author: Muhammad Amjad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-08 05:56:23