下面是我用来更新broker数据的表单,在表单里面有一个单选項radio的值需要输入,我把数据库的值取出来,然后填充到到input項。但是唯有radio这个type的属性項没有被checked。所以我就写了一段script,想在弹出更新表格的时候,这个radio属性能被check。但是没作用。请教一下应该怎么做,才能将传进去的type属性体现出来。
addBrokerForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<meta charset="UTF-8">
<title>add Broker</title>
</head>
<script>
if(broker.type == "E"){
document.getElementById("E").checked=true;
}else{
document.getElementById("J").checked=true;
}
</script>
<body>
<h1>add broker</h1>
<form name=“addBroker” action="/people/saveBroker" method="post">
<spring:bind path="broker.personId" >
<input type="hidden" name="${status.expression}" value="${status.value}"><br/>
</spring:bind>
<spring:bind path="address.personId" >
<input type="hidden" name="${status.expression}" value="${status.value}"><br/>
</spring:bind>
<spring:bind path="address.addressId" >
<input type="hidden" name="${status.expression}" value="${status.value}"><br/>
</spring:bind>
<spring:bind path="broker.code">
<label>Person Code:</label>
<input type="text" onblur="validatePersonCode(this.value)" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="broker.firstName">
<label>First Name:</label>
<input type="text" onblur="validateFirstName(this.value)" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="broker.lastName">
<label>Last Name:</label>
<input type="text" onblur="validateLastName(this.value)" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="broker.type">
<label>Broker Type:</label>
<input type="radio" name= "type" id="E" value="E" /> Expert <br />
<input type="radio" name= "type" id="J" value="J" /> Junior <br />
</spring:bind>
<spring:bind path="broker.secIdentifier">
<label>Broker Second Identifier:</label>
<input type="text" onblur="validateSecIdentifier(this.value)" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.street">
<label>Street:</label>
<input type="text" onblur="validateStreet(this.value)" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.country">
<label>Country:</label>
<input type="text" onblur="validateCountry(this.value)" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.state">
<label>State:</label>
<input type="text" onblur="validateState(this.value)" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<input type="submit" value="Save"/>
</form>
<a href="${pageContext.request.contextPath}/people/broker">Back to List</a>
</body>
<script type="text/javascript" src= "<%=basePath %>js/BrokerValidation.js"></script>
</html>
PersonController
package controller;
import java.util.List;
@Controller
@RequestMapping("/people")
public class PersonController {
@Autowired
private PortfolioService service;
@PostMapping("/saveBroker")
public String saveBroker(@ModelAttribute("broker") Broker tempbroker,
@ModelAttribute("address") Address tempAddress) {
service.addBroker(tempbroker, tempAddress);
return "redirect:/people/broker";
}
@GetMapping("/updateBroker")
public String updateBroker(Model theModel, @RequestParam("personId") Integer personId) {
Broker broker = service.getBrokerById(personId);
Address address = service.getAddressByPid(personId);
theModel.addAttribute("broker", broker);
theModel.addAttribute("address", address);
return "addBrokerForm";
}
}