programing

jQuery에서 모든 확인란 찾기

javajsp 2023. 8. 22. 21:56

jQuery에서 모든 확인란 찾기

확인란 목록이 있습니다.

<input type="checkbox" name="answer" id="id_1' value="1" />
<input type="checkbox" name="answer" id="id_2' value="2" />
...
<input type="checkbox" name="answer" id="id_n' value="n" />

선택된 확인란의 모든 값을 수집할 수 있습니다. 질문은 선택되지 않은 확인란의 모든 값을 어떻게 가져올 수 있습니까?노력했습니다.

$("input:unchecked").val();

선택 취소된 확인란의 값을 가져오지만 다음과 같은 값을 받았습니다.

구문 오류, 인식할 수 없는 식을 선택하지 않았습니다.

누가 이 문제를 밝혀줄 수 있습니까?감사해요!

오류 메시지에 나와 있듯이, jQuery는 다음을 포함하지 않습니다.:unchecked선택자
대신, 당신은 그것을 뒤집어야 합니다.:checked선택기:

$("input:checkbox:not(:checked)")

$("input:checkbox:not(:checked)")선택하지 않은 상자를 가져옵니다.

또한 다음과 같은 방법으로 순수한 j를 사용하여 달성할 수 있습니다.

var matches = document.querySelectorAll('input[type="checkbox"]:not(:checked)');

jQuerys 기능을 확장하여 이를 수행할 수 있습니다.그러면 선택기에 대해 작성해야 하는 텍스트의 양이 줄어듭니다.

$.extend($.expr[':'], {
        unchecked: function (obj) {
            return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
        }
    }
);

그러면 다음을 사용할 수 있습니다.$("input:unchecked")선택한 모든 확인란 및 라디오 단추를 가져옵니다.

$("input[type='checkbox']:not(:checked):not('\#chkAll\')").map(function () { 
   var a = ""; 
   if (this.name != "chkAll") { 
      a = this.name + "|off"; 
   } 
   return a; 
}).get().join();

이렇게 하면 모든 확인란이 검색되고 모든 확인란의 선택을 취소하는 데 사용하는 "chkAll" 확인란이 제외됩니다.데이터베이스에 전달하는 값이 무엇인지 알고 싶으므로 확인란에 on 값이 표시되므로 이 값을 off로 설정합니다.

//looking for unchecked checkboxes, but don’t include the checkbox all that checks or unchecks all checkboxes
//.map - Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.

//.get - Retrieve the DOM elements matched by the jQuery object.
//.join - (javascript) joins the elements of an array into a string, and returns the string.The elements will be separated by a specified separator. The default separator is comma (,).

다음과 같이 사용할 수 있습니다.

$(":checkbox:not(:checked)")

선택 기준class다음을 수행할 수 있습니다.

$("input.className:checkbox:not(:checked)")
$(".clscss-row").each(function () {
if ($(this).find(".po-checkbox").not(":checked")) {
               // enter your code here
            } });

언급URL : https://stackoverflow.com/questions/8465821/find-all-unchecked-checkboxes-in-jquery