[英]How to check if at least one checkbox is checked from list of checkboxes with it name attribute as array
[英]How to check at least one checkbox is checked from the array of checkboxes and the text area is not blank on checkbox with value “other” is checked?
我正在遵循HTML代碼:
<form name="question_issue_form" id="question_issue_form" action="question_issue.php">
<table class="trnsction_details" width="100%" cellpadding="5">
<tbody>
<tr>
<td></td>
<td>
<input type="checkbox" name = "que_issue[]" value = "Question is wrong" id ="chkQueWrong">Question is wrong</input>
</td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Answers are wrong" id ="chkAnsWrong">Answers are wrong</input></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Question direction is incorrect" id ="chkDirIncorrect">Question direction is incorrecct</input></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Other" id ="chkOther">Other</input></td>
</tr>
<tr>
<td></td>
<td class="set_message" style="display:none;"><textarea name="que_issue_comment" rows="4" cols="25" maxlength="100"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" id="report_question_issue"/></td>
</tr>
</tbody>
</table>
</form>
現在,我需要檢查是否從復選框數組中至少選中了一個復選框,並且也僅當選中值“ Other”的復選框時,才檢查文本區域是否被填充。 如果在選中最后一個復選框后為空,則給出錯誤消息。 所有這些錯誤消息都應出現在窗體上,沒有任何警報。
這樣的東西?
var checkbox = document.getElementsByName("que_issue[]");
var checkedAtLeastOne = false;
$('input[type="checkbox"]').each(function () {
if ($(this).is(":checked")) {
//atleast one is checked
checkedAtLeastOne = true;
} else {
//error
document write bla bla bla
}
}
var other = document.getElementById("chkOther");
var textfield = document.getElementByName("que_issue_comment") if ($(other).is(":checked")) {
if ($(textfield).val().length == 0) {
//error
document write bla bla bla
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.