我有多個textareas,我試圖檢查它們是否都是空的,然後應該提示用戶要求他檢查他的條目並且至少有一個文本區域被填滿。檢查多個textareas如果爲空
function checkEvent() {
console.log("infunc");
if (document.getElementById('events_yes').checked) {
document.getElementById('if_events_yes').style.display = "block";
} else {
document.getElementById('if_events_yes').style.display = "none";
}
}
function checkText() {
var content = document.getElementsByName("firewall").value.trim() + document.getElementsByName("ids").value.trim() +
document.getElementsByName("proxy").value.trim() + document.getElementsByName("dpi").value.trim() +
document.getElementsByName("net_malware").value.trim() + document.getElementsByName("av").value.trim() +
document.getElementsByName("forensics").value.trim() + document.getElementsByName("tripwire").value.trim() +
document.getElementsByName("memdumps").value.trim() + document.getElementsByName("email_logs").value.trim();
alert(content.length);
}
<form action="ResponseDB" method="post">
<input name="tipid" value="<% out.println(tipid);%>" hidden="">Any events to report?
<br>
<input type="radio" name="events" onclick="javascript:checkEvent();" id="events_yes" value="yes">Yes
<br>
<input type="radio" name="events" onclick="javascript:checkEvent();" id="events_no" value="no" checked>No
<br>
<div id="if_events_yes" hidden>
<br>
<br>Firewall:
<br>
<textarea rows="5" cols="80" type="text" name="firewall" id="if_events_yes"></textarea>
<br>IDS/IPS:
<br>
<textarea rows="5" cols="80" type="text" name="ids" id="if_events_yes"></textarea>
<br>Web Content Filtering/Proxy:
<br>
<textarea rows="5" cols="80" type="text" name="proxy" id="if_events_yes"></textarea>
<br>Deep packet inspection:
<br>
<textarea rows="5" cols="80" type="text" name="dpi" id="if_events_yes"></textarea>
<br>Network malware protection devices (FireEye, Damballa, etc.):
<br>
<textarea rows="5" cols="80" type="text" name="net_malware" id="if_events_yes"></textarea>
<br>Anti-virus software:
<br>
<textarea rows="5" cols="80" type="text" name="av" id="if_events_yes"></textarea>
<br>Forensics Tools:
<br>
<textarea rows="5" cols="80" type="text" name="forensics" id="if_events_yes"></textarea>
<br>Tripwire:
<br>
<textarea rows="5" cols="80" type="text" name="tripwire" id="if_events_yes"></textarea>
<br>Memory Dumps:
<br>
<textarea rows="5" cols="80" type="text" name="memdumps" id="if_events_yes"></textarea>
<br>Email logs:
<br>
<textarea rows="5" cols="80" type="text" name="email_logs" id="if_events_yes"></textarea>
<br>
</div>
<input type="submit" onclick="javascript:checkText();" value="Submit">
</form>
我缺少什麼或者做錯了嗎?當我點擊提交沒有任何反應時,表單即使被提交也將被提交
您不能擁有多個ID,並且 - 如果發現空textarea,您應該防止提交時的默認行爲...... – sinisake