gpt4 book ai didi

java - 当您单击 HTML 中的

转载 作者:行者123 更新时间:2023-12-02 02:08:11 25 4
gpt4 key购买 nike

你看,我脑子里有这个问题,这很简单。我遇到过这样的情况:我将 servlet 中的值传递给我事先准备好的参数。我的代码已连接到 SQL 数据库,并且有一个删除和编辑按钮。所以基本上,我会使用 ID(在我的例子中准确地说是公司 ID)设置按钮的值。现在,如果单击其中一个按钮?我可以有一个条件语句 if(button1 != null) 吗?或不?因为我有相同的值(value)?我必须有 1 个空按钮,以便代码知道要做什么。我担心即使只单击一个按钮,我设置的值也会被获取。

<%@ 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中。

关于java - 当您单击 HTML 中的 <button>/&lt;input&gt; 时,到底会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50425993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com