美好的一天!
我有一个违规和表单列表,这些违规行为显示在下拉列表中。列表“违规”有2个属性:violationDescription和idViolation。当我选择一个violationDescription并按下按钮保存我需要得到它的idViolation并将其发送到控制器。我需要它向我的表Post添加一个新的违规,它收集违规,日期和其他信息。我怎么能这样做?
List<ViolationsEntity> violation = violationsDao.findAllByOrderByViolationDescriptionAsc();
model.addObject("violations", violation);
form.jsp
<form:select path="classificators">
<form:option value="NONE" label="--- Select violation---" />
<c:forEach items="${violations}" var="violation">
<form:option value="${violation.violationDescription}"/>
</c:forEach>
</form:select>
答案 0 :(得分:1)
这是解决方案,希望它可以帮助某人
$(function () {
var violations = {
<c:forEach items="${violations}" var="violation" varStatus="violationId">
'${violation.idViolation}': '<c:out value="${violation.violationDescription}"/>',
</c:forEach>
};
$("select")
.change(function () {
var str = "";
$( "select option:selected" ).each(function () {
str += $( this ).val();
});
console.log(violations[str])
$( "#output" ).text( violations[str] );
})
})
<div id="output"></div>