그래서 jQuery와 함께 작동하여 체크하지 않거나 체크하지 않는 약간의 가짜 체크 박스가 있습니다. 이 내 문서에 가짜 체크 박스의 숫자가 있으며, 각각에 대해 내가 클릭 기능이 있습니다jQuery (x == y)가 작동하지 않는 경우
var productInterest = [];
productInterest[0] = false;
productInterest[1] = false;
productInterest[2] = false;
// here is one function of the three:
$('#productOne').click(function() {
if (productInterest[0] == false) {
$(this).addClass("checkboxChecked");
productInterest[0] = true;
} else {
$(this).removeClass("checkboxChecked");
productInterest[0] = false;
}
});
문제가 확인되므로 if 문에서 에러가 발생했을 것 같다
있지만 선택을 취소하십시오. 즉, 클래스를 추가하지만 변수는 변경되지 않으므로 여전히 해당 변수가 검사됩니다. 아무도 아이디어가 없나요? 당신의 도움을 주셔서 감사합니다.
업데이트 : 그래서 내가 제공 한 방식으로 작동하므로 내 코드를 모두 보여줄 필요가 있습니다. 대지. 아래에서 코드 전체를 찾으십시오.
각 확인란의 UI와 데이터를 한 번에 업데이트해야하므로 모든 기능이 하나의 기능으로 이루어져야합니다.
$('input[name=silkInterest]').click(function() { // they all have the same name
var silkInterest = [];
silkInterest[0] = false;
silkInterest[1] = false;
silkInterest[2] = false;
if ($(this).is('#silkSilk')) { // function stops working because the .is()
if (silkInterest[0] == false) {
$(this).addClass("checkboxChecked");
silkInterest[0] = true;
} else {
$(this).removeClass("checkboxChecked");
silkInterest[0] = false;
}
alert(silkInterest[0]);
}
if ($(this).is('#silkAlmond')) {
if (silkInterest[1] == false) {
$(this).addClass("checkboxChecked");
silkInterest[1] = true;
} else {
$(this).removeClass("checkboxChecked");
silkInterest[1] = false;
}
}
if ($(this).is('#silkCoconut')) {
if (silkInterest[2] == false) {
$(this).addClass("checkboxChecked");
silkInterest[2] = true;
} else {
$(this).removeClass("checkboxChecked");
silkInterest[2] = false;
}
}
var silkInterestString = silkInterest.toString();
$('input[name=silkInterestAnswer]').val(silkInterestString);
// This last bit puts the code into a hidden field so I can capture it with php.
});
코드가 제대로 작동합니다. (실제로 붙여 넣기를 3 번 복사하는 대신 루프를 사용해야 함) – david
click 이벤트가 발생한 후 값이 실제로 변경되었는지 여부를 알리십시오. – JohnP
코드를 복사하여 붙여 넣었습니다. 정상적으로 작동했습니다. 아마도 페이지의 다른 코드가 잘못된 것입니까? –