简体   繁体   中英

Spring-MVC: JSP <form action> view sends series of values to the controller instead of one value of the button clicked

<form name="update" id="update" action="update" method="post">
<table border="0">
<tr>
<th></th>
<th>Del</th>
<th>Name</th>
<th>Address</th>
<th>Remarks</th>
</tr>

<c:forEach items="${recList}" var="rec" varStatus="loop">
<tr>
<td><input type="checkbox" name="del" value="${loop.index}"/></td>
<td><input type="text" name="name" value="${rec.getName()}"/></td>
<td><input type="text" name="address" value="${rec.getAddress()}"/></td>
<td><input type="text" name="remarks" value="${rec.getRemarks()}"/></td>
<td><input type="hidden" name="index" value="${loop.index}"/>
<input type="submit" value="Update"/></td>
</tr>
</c:forEach>

</table>
</form>

This is my code for this view

在此处输入图片说明

My problem is when I click the UPDATE button, the form seems to send series of values like

name= "aiko,hashimoto,yuki,ode"

instead of sending only

name="hashimoto"

to the Controller if second UPDATE button is clicked. Can anyone help me with my issue? <form> tag can't be inside <table> . By logic, I think it's best to put the inside the loop but it is not possible. Is there another way to solve this?

    Submit your form with javascript function using ajax call like below sample code.
    send single value from form submit not possible 

    function submitform(){

         var messageId = $("#messageId").val();
        $.ajax({
            url:'<c:url value="/dummy/dummyMessageId"/>',
            datatype:'JSON',    
            data: 'messageId=' + messageId ,
            success : function(response) {
            if (null != response && response.success) {      
              } else {
                    var errorDiv = $('#Error');
                    errorDiv.html(response.error.message);
                    }
                }               
        });
    }
    <input type="button" value="Update" onclick="submitform()"/></td> 
   add above code  in your HTML input button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM