2

I have a form with elements like:

<div class="row1">
   <select name="someSelectField" multiple="multiple" class="selectList">
      <option value="1">1</option>          
      <option value="2">2</option>          
   </select>
   <input type="checkbox" name="checkboxField" class="checkboxField" value="1" /> checkbox</td>
</div>
<div class="row2">
   <select name="someSelectField" multiple="multiple" class="selectList">
      <option value="1">1</option>          
      <option value="2">2</option>          
   </select>
   <input type="checkbox" name="checkboxField" class="checkboxField" value="1" /> checkbox</td>
</div>

These will continue down the page. With lots of different rows of these same fields. There is no set rows as javascript is used to create more rows.

My question is, what should the value of each name attribute be, so that it stays grouped as arrays and can be referenced together? For example, if I use someSelectField[1], when the method is GET, the form sends it like someSelectField[1]=1&someSelectfield[1]=2 (if both are selected). I am looking for it to be someSelectField=1&someSelectfield=2, etc.

Any help would be appreciated. Thanks.

3
  • Seems like what you've got above works as is: jsfiddle.net/gilly3/V8sQZ Commented Sep 20, 2011 at 20:22
  • The problem with the above is that input checkboxes will overwrite and not be an array. Those values will differ from row to row. Commented Sep 20, 2011 at 20:28
  • did you try adding [] at the end of the names? that should create an array. for example name="someSelectField[]" Commented Sep 20, 2011 at 20:44

1 Answer 1

1

Why don't you just give the controls unique names? If your javascript is capable of giving the divs indexed class names, you could just restructure the html as so:

<div class="row1">
   <select name="someSelectField1" multiple="multiple" class="selectList">
      <option value="1">1</option>          
      <option value="2">2</option>          
   </select>
   <input type="checkbox" name="checkboxField1" class="checkboxField" value="1" /> checkbox</td>
</div>
<div class="row2">
   <select name="someSelectField2" multiple="multiple" class="selectList">
      <option value="1">1</option>          
      <option value="2">2</option>          
   </select>
   <input type="checkbox" name="checkboxField2" class="checkboxField" value="1" /> checkbox</td>
</div>

This has the benefit of being server independent too since some frameworks parse html arrays really well and give you a proper array, and some just overwrite keys.

Your parsing might get a little messy on the back-end if you're allowed to remove elements from the middle instead of just from the end but it shouldn't be too bad. And if that is a concern you could write a simple jQuery script that would line all your names up again. Warning, untested code follows:

$("div[class^='row']").each(function(idx, value) {
    var $value = $(value);
    $value.find(".selectList").attr("name", "someSelectField"+(idx+1));
    $value.find(".checkboxField").attr("name", "checkboxField"+(idx+1));
});
Sign up to request clarification or add additional context in comments.

3 Comments

The problem with this is I need to send a variable with the number of rows on submit. The jsp is setup to get the select fields like: String[] someSelectField = request.getParameterValues("someSelectField");
I can't really comment on how you interact with web forms through Java, but you should probably add Java to your tags if that's what you're using.
I did this and manipulated it with a JS function on submit. Thanks.

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.