2017-05-31 6 views
0

Guten Tag!Hinzufügen zu DB ausgewählten Wert in JSP-Dropdown mit JSTL

Ich habe eine Liste von Verstößen und Formular, wo diese Verstöße in der Dropdown-Liste angezeigt werden. List "violation" hat 2 Attribute: violationDescription und idViolation. Wenn ich eine der violationDescription auswähle und die Taste Save drücke, muss ich die IDViolation abrufen und an den Controller senden. Ich brauche es, um meiner Tabelle einen neuen Verstoß hinzuzufügen, der Verstöße, Daten und andere Informationen sammelt. Wie kann ich das machen?

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> 

Antwort

1

Hier ist die Lösung, hoffe, es kann helfen jemand

$(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> 
Verwandte Themen