0

Good day!

I have a list of violations and form, where this violations are shown in drop down list. List "violation" has 2 attributes: violationDescription and idViolation. When I select one of violationDescription and press button Save I need to get it's idViolation and send it to controller. I need it to add a new violation to my table Post, which collect violations, dates and other information. How can I do that?

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>

1 Answer 1

1

Here is the solution, hope it can helps someone

$(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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.