简体   繁体   中英

How to disable Form Onsubmit function JSP

I have two radio buttons, (Yes and No) when the user choses Yes, a set of text boxes will appear and is required to have at least one entry in order to submit. If the user chose "No" he/she can submit with no entries or whatsoever. I have everything working except for the "No" part. The validation function works inside the form, is there anyway to disable it when the chosen button is "No"?

  <script type="text/javascript">

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";
        document.getElementById('myForm').removeAttribute("onsubmit","");

    }
}
</script>

<form action="ResponseDB"  method="post" id="myForm">
<input name="tipid" value="<% out.println(tipid);%>" type = "hidden">
Any events to report?<br>
<input type="radio" name="events" onclick="checkEvent()" id="events_yes" value="no">Yes<br>
<input type="radio" name="events" onclick="checkEvent()" id="events_no" value="yes" checked>No<br>


<div id="if_events_yes" style="display:none">
<br><br>
Firewall:<br>
<textarea rows="5" cols="80" name="firewall"></textarea><br>
IDS/IPS:<br>
<textarea rows="5" cols="80" name="ids"></textarea><br>
Web Content Filtering/Proxy:<br>
<textarea rows="5" cols="80" name="proxy"></textarea><br>
Deep packet inspection:<br>
<textarea rows="5" cols="80" name="dpi"></textarea><br>
Network malware protection devices (FireEye, Damballa, etc.):<br>
<textarea rows="5" cols="80" name="net_malware"></textarea><br>
Anti-virus software:<br>
<textarea rows="5" cols="80" name="av" id="av"></textarea><br>
Forensics Tools:<br>
<textarea rows="5" cols="80" name="forensics"></textarea><br>
Tripwire:<br>
<textarea rows="5" cols="80" name="tripwire"></textarea><br>
Memory Dumps:<br>
<textarea rows="5" cols="80" name="memdumps"></textarea><br>
Email logs:<br>
<textarea rows="5" cols="80" name="email_logs"></textarea><br>



    </div>
    <input type="submit" value="Submit">
</form>

<script>

    onsubmit=function() {
        var t = document.getElementsByTagName("textarea"),
            l = 0;
               for(var i = 0; i < t.length; i++){
                l = l + t[i].value.trim();
               }
                if (l < 1) {
                            alert("Please have at least one entry");
                return false;
        }

    }
</script>

This is the portion I'm working on. Is there anyway I can disable the onsubmit function when the user choses No?

You should be able to use this example from w3schools . Their example sends to an asp page, but that doesn't matter.

I have copied the part you need here, but for more details on how or why it works visit the link above.

JavaScript Part

a JavaScript function like this but checking the state of your radio buttons:

function validateForm() {
 //here just get the value(s) from your radio(s)
 var x = document.forms["myForm"]["fname"].value;
 //check the value
 if (x == null || x == "") {
     alert("Name must be filled out");
     //this prevents the submit to your server
     return false;
 }
}

Form Part

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">

It is working thanks to Kevin2 from webdeveloper.com forums....

   <%@page import="com.sun.xml.internal.txw2.Document"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="db.ResponseTracker"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MASC Response Form</title>

<style>
/* this selector could be div#nav instead of nav depending on which tag you wrapped the ul in */
#nav {
    width: 100%;
    float: left;
    margin: 0 0 1em 0;
    padding: 0;
    background-color: #f2f2f2;
    border-bottom: 1px solid #ccc;  }
#nav ul {
    list-style: none;
    width: 800px;
    margin: 0 auto;
    padding: 0; }
#nav li {
    float: left; }
#nav li a {
    display: block;
    padding: 8px 15px;
    text-decoration: none;
    font-weight: bold;
    color: #069;
    border-right: 1px solid #ccc; }
#nav li:first-child a {
    border-left: 1px solid #ccc; }
#nav li a:hover {
    color: #c00;
    background-color: #fff; }

#tipid, fieldset {
    display: none;
}
label {
    display: block;
}
fieldset {
    border: 0;
    width: 50%;
}
textarea {
    width: 100%;
    height: 5em;
}
.active {
    position: fixed;
    left: 54%;
    bottom: 2em;
}
</style>

<script src="nav.jsp"></script>

</head>
<body>
<div id="nav">
    <ul>
        <li><a href="welcome.jsp">My Data</a></li>
        <li><a href="logout.jsp">Logout</a></li>
    </ul>
</div>

<% ResponseTracker tracker = new ResponseTracker() ;
    int tipid = 0;
    if(request.getParameter("tipid") != null)
    {
        tipid = Integer.parseInt(request.getParameter("tipid"));    

    }

    if (tipid == 0){ response.sendRedirect("/MASC/index.jsp");}

    %>
<h1> Tip report for <% out.print(tracker.getDateStringByTipId(tipid)); 
        tracker.finalize();%></h1>
    <%if(request.getParameter("failed") != null)
    {
        %> <br><font color=\"red\"> Login failed</font> <%
    }
%>
<form action="ResponseDB" method="post">
    <input name="tipid" id="tipid" value="<% out.println(tipid);%>">
    Any events to report?
    <label><input type="radio" name="events" onclick="checkEvent(this.value,if_events_yes,submit)" id="events_yes" value="yes">Yes</label>
    <label><input type="radio" name="events" onclick="checkEvent(this.value,if_events_yes,submit)" id="events_no" value="no" checked>No</label>

    <fieldset id="if_events_yes">

    <label>Firewall:<br>
    <textarea name="firewall"></textarea></label>

    <label>IDS/IPS:<br>
    <textarea name="ids"></textarea></label>

    <label>Web Content Filtering/Proxy:<br>
    <textarea name="proxy"></textarea></label>

    <label>Deep packet inspection:<br>
    <textarea name="dpi"></textarea></label>

    <label>Network malware protection devices (FireEye, Damballa, etc.):<br>
    <textarea name="net_malware"></textarea></label>

    <label>Anti-virus software:<br>
    <textarea name="av"></textarea></label>

    <label>Forensics Tools:<br>
    <textarea name="forensics"></textarea></label>

    <label>Tripwire:<br>
    <textarea name="tripwire"></textarea></label>

    <label>Memory Dumps:<br>
    <textarea name="memdumps"></textarea></label>

    <label>Email logs:<br>
    <textarea name="email_logs"></textarea></label>

    </fieldset>

    <input type="submit" id="submit" value="Submit">
</form>

<script>

function checkEvent(el,y,s) {
    console.log("infunc");
    if (el == 'yes') {
        y.style.display = "block";
        s.className = "active";
    }
    else {
        y.style.display = "none";
        s.className = "";
    }
}

onsubmit=function() {
    if (document.getElementById('events_yes').checked == true) {
    var t = document.getElementsByTagName("textarea"),
        l = 0;
        for(var i = 0; i < t.length; i++){
            l = l + t[i].value.trim();
        }
            if (l < 1) {
                    alert("Please re-enter");
        return false;
    }
    }
}
</script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM