jQuery ( jquery를 사용하여 여러 확인란 값을 얻는 방법 )를 사용하여 여러 개의 확인란 값 을 가져올 수 있지만 확인란 입력이 html 양식 안에 있으므로 해당 jQuery 솔루션이 작동하지 않는다는 것을 이해합니다. 양식 내에서 확인란 값.
양식에서 값을 추출하려고 시도했지만 값이 아닌 확인란의 이름이 문서에 나타나는 횟수를 세는 것처럼 보이는 이상한 라디오 노드 목록을 만듭니다.
function validateForm() {
var checks = document.forms["TestForm"]["ParticipantSelection[]"];
alert(checks);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<form name="TestForm" action="Video1.php" method="post" onsubmit="return validateForm()">
<table>
<tr>
<th><img name="<?php echo $valueindicator[0];?>" src="<?php echo $all_four_images[0];?>" height="100">
</th>
<th><img name="<?php echo $valueindicator[1];?>" src="<?php echo $all_four_images[1];?>" height="100">
</th>
<th><img name="<?php echo $valueindicator[2];?>" src="<?php echo $all_four_images[2];?>" height="100">
</th>
</tr>
<tr>
<th> <input type="checkbox" name="ParticipantSelection[]" value="image1">
</th>
<th><input type="checkbox" name="ParticipantSelection[]" value="image2">
</th>
<th><input type="checkbox" name="ParticipantSelection[]" value="image3">
</th>
</tr>
</table>
<input type="hidden" name="AnswerCondition" value="BrowserCheckAnswer">
<button type="submit">Continue</button>
</form>
그러나 나는 js가 다른 것을 계산하는 대신 양식 입력 내부의 값을 얻는 방법을 모르고 체크 상자의 값에 액세스하는 방법을 모릅니다.
jQuery .map()
함수를 사용하여 확인 된 각 checkbox
입력 유형 을 반복 할 수 있습니다 . selected
체크 박스가 있는 객체를 반환합니다 . 이제 jQuery 객체 에서 배열을 가져 오기 위해 다음 .get()
과 같은 메서드를 사용할 수 있습니다 .
이 시도:
var validateForm = function() {
var checks = $('input[type="checkbox"]:checked').map(function() {
return $(this).val();
}).get()
console.log(checks);
return false;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="TestForm" action="Video1.php" method="post" onsubmit="return validateForm()">
<table>
<tr>
<th> <input type="checkbox" name="ParticipantSelection[]" value="image1">
</th>
<th><input type="checkbox" name="ParticipantSelection[]" value="image2">
</th>
<th><input type="checkbox" name="ParticipantSelection[]" value="image3">
</th>
</tr>
</table>
<input type="hidden" name="AnswerCondition" value="BrowserCheckAnswer"/>
<button type="submit">Continue</button>
</form>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다