jQuery : 확인란이 선택되어 있지 않은지 테스트
이 문제를 파악하는 데 문제가 있습니다. 두 개의 체크 박스가 있습니다 (향후에는 더 추가 될 예정입니다).
checkSurfaceEnvironment-1
checkSurfaceEnvironment-2
기본적으로 if 문을 작성하고 그중 하나가 확인되고 다른 하나는 확인되지 않았는지 테스트하고 싶습니다. 다음을 수행하는 가장 쉬운 방법은 무엇입니까?
if ( $("#checkSurfaceEnvironment-1").attr('checked', true) &&
$("#checkSurfaceEnvironment-2").is('**(NOT??)** :checked') ) {
// do something
}
내가 사용하는 신뢰할 수있는 방법은 다음과 같습니다.
if($("#checkSurfaceEnvironment-1").prop('checked') == true){
//do something
}
선택된 요소를 반복하려면 상위 요소를 사용하십시오.
$("#parentId").find("checkbox").each(function(){
if ($(this).prop('checked')==true){
//do something
}
});
더 많은 정보:
이것은 모든 체크 박스에 체크 박스의 실제 상태를 저장하는 속성이 있기 때문에 잘 작동합니다. 원하는 경우 페이지를 검사하고 확인란을 선택 및 선택 취소하면 "선택됨"(있는 경우) 속성이 동일하게 유지됩니다. 이 속성 은 현재 상태가 아닌 체크 박스 의 초기 상태 만을 나타냅니다 . 현재 상태는 해당 확인란에 대한 dom 요소의 확인 된 속성에 저장됩니다.
if (!$("#checkSurfaceEnvironment-1").is(":checked")) {
// do something if the checkbox is NOT checked
}
jQuery .not()
메서드 또는 :not()
선택기를 사용할 수도 있습니다 .
if ($('#checkSurfaceEnvironment').not(':checked').length) {
// do stuff for not selected
}
추가 참고 사항
:not()
선택기에 대한 jQuery API 문서에서 :
이 아니요 () 메소드는 에 복잡한 선택기 또는 변수를 밀어 것보다 더 읽기 선택 당신을 제공 끝날 하지 () : 선택 필터. 대부분의 경우 더 나은 선택입니다.
다른 방법 :
여기에 작동 예제가 있고 여기에 코드가 있습니다. 또한 prop을 사용해야합니다.
$('input[type="checkbox"]').mouseenter(function() {
if ($(this).is(':checked')) {
//$(this).prop('checked',false);
alert("is checked");
} else {
//$(this).prop('checked',true);
alert("not checked");
}
});
체크 된 속성을 토글하는 방법을 설명했습니다.
if ( $("#checkSurfaceEnvironment-1").is(":checked") && $("#checkSurfaceEnvironment-2").not(":checked") )
avijendr가 제안한 것과 같은보다 직접적인 구현을 찾고있었습니다.
나는 그의 / 그녀의 구문에 약간의 문제가 있었지만 작동하도록했습니다.
if ($('.user-forms input[id*="chkPrint"]:not(:checked)').length > 0)
내 경우, 나는 클래스와 테이블을했다 user-forms
, 그리고 문자열을 가지고 체크 박스의 경우 나는 확인 된 checkPrint
자신의에서이 id
체크되지 않은했다.
To do it with .attr()
like you have, to see if it's checked it would be .attr("checked", "checked")
, and if it isn't it would be .attr("checked") == undefined
Here I have a snippet for this question.
$(function(){
$("#buttoncheck").click(function(){
if($('[type="checkbox"]').is(":checked")){
$('.checkboxStatus').html("Congratulations! "+$('[type="checkbox"]:checked').length+" checkbox checked");
}else{
$('.checkboxStatus').html("Sorry! Checkbox is not checked");
}
return false;
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<p>
<label>
<input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_0">
Checkbox</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1_" value="checkbox" id="CheckboxGroup1_1">
Checkbox</label>
<br>
</p>
<p>
<input type="reset" value="Reset">
<input type="submit" id="buttoncheck" value="Check">
</p>
<p class="checkboxStatus"></p>
</form>
I think the easiest way (with jQuery) to check if checkbox is checked or NOT is:
if 'checked':
if ($(this).is(':checked')) {
// I'm checked let's do something
}
if NOT 'checked':
if (!$(this).is(':checked')) {
// I'm NOT checked let's do something
}
Return true if all checbox are checked in a div
function all_checked (id_div){
all_checked = true;
$(id_div+' input[type="checkbox"]').each(function() {
all_checked = all_checked && $('this').prop('checked');
}
return all_checked;
}
Simple and easy to check or unchecked condition
<input type="checkbox" id="ev-click" name="" value="" >
<script>
$( "#ev-click" ).click(function() {
if(this.checked){
alert('checked');
}
if(!this.checked){
alert('Unchecked');
}
});
</script>
try this one
if ($("#checkSurfaceEnvironment-1:checked").length>0) {
//your code in case of NOT checked
}
In Above code selecting the element by Id (#checkSurfaceEnvironment-1)
then filter out it's checked state by (:checked)
filter.
It will return array of checked element object. If there any object exists in the array then if condition will be satisfied.
There are two way you can check condition.
if ($("#checkSurfaceEnvironment-1").is(":checked")) {
// Put your code here if checkbox is checked
}
OR you can use this one also
if($("#checkSurfaceEnvironment-1").prop('checked') == true){
// Put your code here if checkbox is checked
}
I hope this is useful for you.
Here is the simplest way given
<script type="text/javascript">
$(document).ready(function () {
$("#chk_selall").change("click", function () {
if (this.checked)
{
//do something
}
if (!this.checked)
{
//do something
}
});
});
</script>
I used this and in worked for me!
$("checkbox selector").click(function() {
if($(this).prop('checked')==true){
do what you need!
}
});
I know this has already been answered, but still, this is a good way to do it:
if ($("#checkbox").is(":checked")==false) {
//Do stuff here like: $(".span").html("<span>Lorem</span>");
}
if($("#checkbox1").prop('checked') == false){
alert('checkbox is not checked');
//do something
}
else
{
alert('checkbox is checked');
}
$("#chkFruits_0,#chkFruits_1,#chkFruits_2,#chkFruits_3,#chkFruits_4").change(function () {
var item = $("#chkFruits_0,#chkFruits_1,#chkFruits_2,#chkFruits_3,#chkFruits_4");
if (item.is(":checked")==true) {
//execute your code here
}
else if (item.is(":not(:checked)"))
{
//execute your code here
}
});
참고URL : https://stackoverflow.com/questions/11440128/jquery-test-if-checkbox-is-not-checked
'development' 카테고리의 다른 글
Java에서 포인터를 어떻게 사용할 수 있습니까? (0) | 2020.08.19 |
---|---|
Xcode 4.5를 사용하여 스토리 보드를 편집 할 때 새로운 종료 아이콘이 사용되는 것을 아는 사람이 있습니까? (0) | 2020.08.19 |
Jquery 확인란 모두 확인 (0) | 2020.08.19 |
완벽한 OOP 애플리케이션을 만드는 방법 (0) | 2020.08.19 |
현재 연도를 어떻게 표시합니까? (0) | 2020.08.19 |