簡體   English   中英

當您單擊<button>/</button>時,實際發生了什么 <button><input></button> <button>在HTML中?</button>

[英]What really happens when you click the <button>/<input> in HTML?

你看,我腦子里有這個問題,這很簡單。 我遇到的一種情況是,我將servlet中的值傳遞給預先准備的參數。 我的代碼已連接到SQL數據庫,並且具有刪除和編輯按鈕。 因此,基本上,我將使用ID(在我的情況下為准確的公司ID)設置按鈕的值。 現在,是否單擊任何一個按鈕? 我可以在if(button1!= null)的地方聲明條件嗎? 或不? 因為我有相同的價值? 我必須有1個null按鈕,以便代碼可以知道該怎么做。 恐怕即使只單擊一個按鈕,也會獲取我設置的值。

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>View Database</title> </head> <body> <h1>View Database</h1> <hr> <br> <br> <hr> <br> <div style="padding-left: 200px"> <table border = "1px" bordercolor = "black"> <tr> <td>Company ID</td> <td>Name</td> <td>Contact</td> <td>Age</td> <td>Gender</td> <td>Email</td> <td>Position</td> <td colspan = "2"> Operations</td> </tr> <c:forEach items = "${modelList}" var = "list"> <tr> <td>${list.cid}</td> <td>${list.name}</td> <td>${list.num}</td> <td>${list.age}</td> <td>${list.gender}</td> <td>${list.email}</td> <td>${list.position}</td> <td><form action = "updateEmployee" method = "post" ><button type="submit" name = "editButton" value = "${list.cid}">Update</button></form></td> <td><form action = "updateEmployee" method = "get" ><button type="submit" onclick="alert('Employee deleted!')" name = "delButton" value = "${list.cid}">Remove</button></form></td> </tr> </c:forEach> </table> </div> <br> <hr> <br> <br> <hr> <p align="right"> <a href="http://localhost:8080/EmployeeWeb/myHome.jsp" target="_self"><small>Home</small> </a> | <a href="http://localhost:8080/EmployeeWeb/MyRegistrationPage.jsp" target="_self"><small>Register</small> </a> | <a href="http://localhost:8080/EmployeeWeb/MyUpdatePage.jsp" target="_self"><small>Update</small> </a> | <a href="http://localhost:8080/EmployeeWeb/MyDeletePage.jsp" target="_self"><small>Delete</small> </a> | <a href="http://localhost:8080/EmployeeWeb/MyAboutPage.jsp" target="_self"><small>About</small> </a> </p> </body> </html> 

你可以在HTML中有這樣的東西

<form action="myapiURL" method="get">
<button name="ID" value="Company1ID" type="submit">Company 1</button>
<button name="ID" value="Company2ID" type="submit">Company 2</button>
<button name="ID" value="Company3ID" type="submit">Company 3</button>
<!-- ... -->
</form>

然后根據按下哪個按鈕,將請求的ID參數設置為該按鈕的值。 那是你需要的嗎?

如果你想沒有刷新頁面,則可以使用AJAX和發送信息,並處理響應。 例如進行刪除:

 <html> <head> <script> function btnClick(btn) { if(confirm("Are you sure you want to delete: "+btn.innerText+"("+btn.id+")")) { // create a request to "up" (is up really the url you are going to send the request to?) var xhr= new XMLHttpRequest(), method = "DELETE", url = "up", data = new FormData(); xhr.open(method, url, true); xhr.onreadystatechange = function () { // With the onreadystatechange we can handle the response if(xhr.readyState === 4){ // we will just show an alert with contents to the response for the example. alert(xhr.responseText); } }; // FormData.append(name, value) - appends a new value onto an // existing key inside a FormData object, or adds the key if // it does not already exist. data.append("companyId", btn.id); data.append("companyName", btn.companyname); // now send the data to "up" xhr.send(data); } } </script> </head> <body> <button onclick="btnClick(this)" id="CompanyID1" companyname="ACME">Delete ACME</button> <button onclick="btnClick(this)" id="CompanyID2" companyname="Sears">Delete Sears</button> <button onclick="btnClick(this)" id="CompanyID3')" companyname="Evil Corp.">Delete Evil Corp.</button> </body> </html> 

當然,這將嘗試將請求發送到不存在的stackoverflow.com/post/5046175/up,因此我們將得到一個空響應。 但是它將發送參數 companyId=<one pressed>companyName=<one pressed>因為我在formdata中設置了它們。

暫無
暫無

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

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