체크되지 않은 체크 박스의 값을 '0'으로 지정해야 체크 된 값과 체크 된 모든 값 ($ _POST [ 'chk])을 확인할 수 있습니다. . 나는 for 루프를 사용하여 체크 박스를 만들고 '0'값을 가진 숨겨진 필드를 생성하고 있습니다 (stackoverflow에 elsewhere이 제안되었습니다).PHP는 체크하지 않은 체크 박스의 값을 얻습니다.
4 개 체크 박스 ((카운트 ($ 결과) -1) = 4) 체크 박스 1 & 3 동안 체크 될 때 체크 상자 2 & 4, 체크 인 $ _POST [ 'CHK'] 배열이 같은 끝이있다 :
Array
(
[chk] => Array
(
[0] => 180.00
[1] => 0
[2] => 100.00
[3] => 0
)
내가 무슨 일을하고있는 중이 야 : 나는 다음과 같이 할
Array
(
[chk] => Array
(
[0] => 0
[1] => 180.00
[2] => 0
[3] => 0
[4] => 100.00
[5] => 0
)
? for 루프와 숨겨진 체크 박스 필드를 사용하여이 작업을 수행 할 수 있습니까?
자바 스크립트 :
<script type="text/javascript">
function calculate() {
var el, i = 0;
var subtotal = 0;
while(el = document.getElementsByName("chk[]")[i++]) {
if(el.checked) { subtotal = subtotal + Number(el.value);}
}
var node = document.getElementById("subtotal");
node.innerHTML = "$" + subtotal + ".00";
var node = document.getElementById("total");
node.innerHTML = "$" + (subtotal*<?=$no_nights?>) + ".00";
}
</script>
HTML/PHP :
<form id="booking_step2" name="booking_step2" method="POST" action="index.php?p=bookings?s=3">
<? for ($x=0; $x<=(count($result)-1); $x++) { ?>
<input type="hidden" name="chk[]" value="0">
<input type="checkbox" name="chk[]" value="<?=$result[$x]['r_rate'];?>" onclick="calculate()">
<? } ?>
자바 스크립트로 무엇을 할 수 있습니까? while (el = document.getElementsByName ("chk []") [i ++]) { – Accolade
@Accolade : 적절한 다른 것으로 바꿉니다. 예를 들어, 모든 입력 요소에 특정 클래스를 부여하고 실제 HTML 인 경우 'document.getElementsByClassName'을 사용하거나'document.querySelectorAll ("# booking_step2> input")'을 사용할 수 있습니다. – Jon
입력에 클래스 이름 추가하기 " >''while (el = document.getElementsByClassName ("blah") [i ++]) {'작동하지 않는 것 같습니까? – Accolade