簡體   English   中英

提交表單時如何更改無顯示以阻止

[英]how to change display none to block when form submits

我正在自己的時間里學習JSP,javascript和一些jquery,所以請不要介意可能可怕的代碼或邏輯,但是我正在制作一個表單,當我提交表單時,我希望它顯示3個標簽來顯示輸入,如在下面的代碼中可以看到,我可以進行提交,但是我似乎無法弄清楚如何將CSS顯示從無更改為阻止。 它可能正盯着我,但我無法弄清楚。

JSP

<html>
<body>

<link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/dylan/css/styles.css" />

<form action="form.jsp" id="test" name="test" method="POST">
<label>Please enter your name:</label>
<input name="Name" id="Name">
<br>
<br>
<label>Please enter your number:</label>
<input name="Num" id="Num">
<br>
<br>
<label>Please select your location:</label>
<select name="Location" id="Location">
  <option value=""></option>
  <option value="USA">USA</option>
  <option value="UK">UK</option>
  <option value="Ireland">Ireland</option>
  <option value="France">France</option>
  <option value="Germany">Germany</option>
</select>

<br>
<br>
<button type="button" id="btnsubmit" name="btnsubmit" onclick="myFunction()">Submit</button>
<br>

<p><label id="lblName" style="display:none;"><% out.println(request.getParameter("Name")); %></label></p>

<p><label id="lblNumber" style="display:none;"><% out.println(request.getParameter("Num")); %></label></p>

<p><label id="lblLocation" style="display:none;"><% out.println(request.getParameter("Location")); %></label></p>

<script type="text/javascript" src="<%=request.getContextPath()%>/dylan/js/form.js"></script>

</form>
</body>
</html>

在標簽中,我將所有三種樣式都設置為none,我不知道如何在提交表單時將其更改為阻止樣式。

CSS

body {
    background-color:#066;
    color:#fff;
}
form {
    width:400px;
    height:240px;
    Background-color:#000;
    margin-left:auto;
    margin-right:auto;
    margin-top:170px;
    Padding:10px;
    border-style:solid;
    border-width:5px;
    border-radius:25px;
    text-align:center;
}

JavaScript的

function myFunction() {
    var mainname, mainnum, mainloc;

    mainname = document.getElementById("Name").value;
    mainnum = document.getElementById("Num").value;
    mainloc = document.getElementById("Location").options[document.getElementById("Location").selectedIndex].value;

    if (mainname == "") {
        alert("Please enter your name");
        return false;
    }

    if (mainnum == "") {
        alert("Please enter your Number");
        return false;
    }

    if (mainloc == '') {
        alert("Please select a country from the list");
        return false;
    }

    document.getElementById("test").submit();
}

您可以將顯示設置為阻止,如下所示。

document.getElementById('lblName').style.display = 'block';
document.getElementById('lblNumber').style.display = 'block';
document.getElementById('lblLocation').style.display = 'block';

如果標簽很多,可以使用:

var labels = document.getElementsByTagName('label');

for(var i=0; i<labels.length; i++){
    labels[i].style.display = 'block';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM