Programming

jquery를 사용하여 특정 클래스 이름을 가진 모든 확인란을 선택하십시오.

procodes 2020. 5. 9. 17:02
반응형

jquery를 사용하여 특정 클래스 이름을 가진 모든 확인란을 선택하십시오.


다음을 사용하여 페이지에서 모든 확인란을 선택할 수 있음을 알고 있습니다.

$('input[type=checkbox]').each(function () {
    var sThisVal = (this.checked ? $(this).val() : "");
});

그러나 나는 이것을 포함하고 싶지 않은 다른 확인란이있는 페이지에서 이것을 사용하고 있습니다. 특정 클래스가있는 확인란을 선택하도록 위의 코드를 어떻게 변경합니까?


$('.theClass:checkbox:checked')클래스와 함께 선택된 모든 확인란을 제공합니다 theClass.


$('input:checkbox.class').each(function () {
       var sThisVal = (this.checked ? $(this).val() : "");
  });

예로 설명합니다.

:checkbox확인란의 선택기입니다 (실제로 input이전 버전의 라이브러리에서 이상한 결과를 얻을 수있는 틈새 사례를 찾았지만 실제로는 선택기 일부를 생략 할 수 있습니다. 나는 그것들이 이후 버전에서 고정되어 있다고 확신합니다). .class를 포함하는 요소 클래스 속성의 선택기입니다 class.


의무적 인 .map예 :

var checkedVals = $('.theClass:checkbox:checked').map(function() {
    return this.value;
}).get();
alert(checkedVals.join(","));

$('input.yourClass:checkbox:checked').each(function () {
    var sThisVal = $(this).val();
});

클래스 이름 "yourClass"의 모든 확인란이 표시됩니다. 나는이 예처럼이의 jQuery 셀렉터를 사용하기 때문에 검사 조건 검사를하고 대신. 개인적으로 배열을 사용하여 값을 저장 한 다음 필요에 따라 다음과 같이 사용합니다.

var arr = [];
$('input.yourClass:checkbox:checked').each(function () {
    arr.push($(this).val());
});

모든 확인란 의 값을 배열로 가져와야 하는 경우 :

let myArray = (function() {
    let a = [];
    $(".checkboxes:checked").each(function() {
        a.push(this.value);
    });
    return a;
})()

 $('input.theclass[type=checkbox]').each(function () {
   var sThisVal = (this.checked ? $(this).val() : "");
 });

 $('input.myclass[type=checkbox]').each(function () {
   var sThisVal = (this.checked ? $(this).val() : ""); });

jQuery 클래스 선택기를 참조하십시오 .


귀하의 특정 경우에 도움이되는지 확실하지 않으며 귀하의 경우 포함하려는 확인란이 모두 단일 양식 또는 div 또는 표의 일부인지 확실하지 않지만 항상 모든 확인란을 선택할 수 있습니다 특정 요소 내부. 예를 들면 다음과 같습니다.

<ul id="selective">
 <li><input type="checkbox" value="..." /></li>
 <li><input type="checkbox" value="..." /></li>
 <li><input type="checkbox" value="..." /></li>
 <li><input type="checkbox" value="..." /></li>
</ul>

그런 다음 다음 jQuery를 사용하여 id = "selective"를 사용하여 해당 UL 내의 확인란 만 선택합니다.

$('#selective input:checkbox').each(function () {
 var sThisVal = (this.checked ? $(this).val() : "");
});

Simple way to get all of values into an array

var valores = (function () {
    var valor = [];
    $('input.className[type=checkbox]').each(function () {
        if (this.checked)
            valor.push($(this).val());
    });
    return valor;

})();

console.log(valores);

You can use something like this:
HTML:

<div><input class="yourClass" type="checkbox" value="1" checked></div>
<div><input class="yourClass" type="checkbox" value="2"></div>
<div><input class="yourClass" type="checkbox" value="3" checked></div>
<div><input class="yourClass" type="checkbox" value="4"></div>


JQuery:

$(".yourClass:checkbox").filter(":checked")


It will choose values of 1 and 3.


<input type="checkbox" id="Checkbox1" class = "chk" value = "1" />
<input type="checkbox" id="Checkbox2" class = "chk" value = "2" />
<input type="checkbox" id="Checkbox3" class = "chk" value = "3" />
<input type="checkbox" id="Checkbox4" class = "chk" value = "4" />
<input type="button" id="demo" value = "Demo" />

<script type = "text/javascript" src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
    $("#demo").live("click", function () {
        $("input:checkbox[class=chk]:checked").each(function () {
            alert("Id: " + $(this).attr("id") + " Value: " + $(this).val());
        });
    });
</script>

http://www.jqueryfaqs.com/Articles/Get-values-of-all-checked-checkboxes-by-class-name-using-jQuery.aspx


I know this has a bunch of great answers on this question already but I found this while browsing around and I find it really easy to use. Thought I'd share for anyone else looking.

HTML:

<fieldset>
    <!-- these will be affected by check all -->
    <div><input type="checkbox" class="checkall"> Check all</div>
    <div><input type="checkbox"> Checkbox</div>
    <div><input type="checkbox"> Checkbox</div>
    <div><input type="checkbox"> Checkbox</div>
</fieldset>
<fieldset>
    <!-- these won't be affected by check all; different field set -->
    <div><input type="checkbox"> Checkbox</div>
    <div><input type="checkbox"> Checkbox</div>
    <div><input type="checkbox"> Checkbox</div>
</fieldset>

jQuery:

$(function () {
    $('.checkall').on('click', function () {
        $(this).closest('fieldset').find(':checkbox').prop('checked', this.checked);
    });
});

Reference: Easiest "Check All" with jQuery


You can try like this

let values = (function() {
                let a = [];
                $(".chkboxes:checked").each(function() {
                    a.push($(this).val());
                });
                return a;
            })();

참고URL : https://stackoverflow.com/questions/5450104/using-jquery-to-get-all-checked-checkboxes-with-a-certain-class-name

반응형