jquery에서 모든 확인란을 선택하십시오. name=”answer” id=”id_2′ value=”2″ /> … <input type=”checkbox” name=”answer”

확인란 목록이 있습니다.

<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)") 선택하지 않은 상자를 가져옵니다.


답변

jQuery 기능을 확장하여이를 수행 할 수 있습니다. 이렇게하면 선택기에 쓸 텍스트 양이 줄어 듭니다.

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

그런 다음 $("input:unchecked")모든 확인란과 라디오 버튼을 선택하여 사용할 수 있습니다 .


답변

또한 다음과 같은 방법으로 순수한 js로 달성 할 수 있습니다.

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

답변

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

이것은 체크되지 않은 모든 체크 박스를 검색하고 모든 체크 박스를 체크 해제하는 데 사용하는 “chkAll”체크 상자를 제외시킵니다. 체크 박스가 나에게 값을 주므로 데이터베이스에 전달하는 값을 알고 싶습니다.

//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)")