As I mentioned in a comment above, the id attribute is supposed to be unique, so creating elements in a loop with the same id is giving you invalid html and $("#testfile")
will select only the first element with that id (except in some browsers that might select only the last).
A better solution for repeated elements is to give them a common class and distinguish between them according to which container they belong to, or by a known relationship to some other element if not the container. Or select by tagname if that is unique enough with the containing element, or by the name
attribute. You seem to be using jQuery, and it makes any of these options easy.
I think you can tidy your code up to something like this:
<table id="yourTableIdHere">
<% for (int i = 0; i < value.size(); i++) { %>
<tr><td>
<html:form action="save" method="post" styleId="update" enctype="multipart/form-data">
<input type=hidden name="secfeaturetype" value="" />
<html:file property="testfile" styleClass="testfile"/>
<input type="button" value="Update" />
</html:form>
</td></tr>
<% }%>
</table>
I'm not really familiar with the Struts html tag library, but I believe styleClass="testfile"
will render an attribute class="testfile"
(if not please substitute whatever the appropriate thing is to render class=...
). Notice I removed your inline onclick=...
attribute. You can assign these handlers in one step with jQuery:
$(document).ready(function() {
// assign a click handler to all the Update buttons that are
// within forms within your table element:
$('#yourTableIdHere form input[type="button"][value="Update"]').click(function() {
var $myForm = $(this).parent(); // or $(this).closest('form'),
$myFileInput = $myForm.find('input.testfile'),
$myHiddenInput = $myForm.find('input[type="hidden"]');
alert($myFileInput.val()); // alert value of file input
$myHiddenInput.val("something"); // set value of hidden
});
});
Within the click handler, this
is the clicked button, so from $(this)
you can navigate to the parent element to get a reference to the form that particular button belongs to, and from that form you can use .find()
to select the file and hidden inputs in the current form.