views:

49

answers:

1

Hi guys, as the question says, I am trying to insert one or more tick box values into one database field, preferably in a comma delimited format.

So if the user chooses tick box one and tick box two, the input into the database will be inserted as tickOne, tickTwo, etc etc

How could I go about doing that? Maybe using jQuery or Javascript?

Thanx in advance!

+2  A: 

If you want to pass the values as a string, say comma delimited like you mentioned in jquery you could set the value using .map(), like this:

$(".mySelector:checkbox").change(function() {
  var values = $(".mySelector:checkbox:checked").map(function() {
                 return this.value;
               }).get().join(', ');
  $("#myHiddenInput").val(values);
});

Every time a checkbox changes, it'll re-do the string portion, re-serializing the result, so on postback it should be the current UI selection. .map() gets an array of the values of the :checked elements with your mySelector class, then we're just doing a .join() to convert that array into a string and using .val() to set the hidden input for this to that string.

This assumes markup like this:

<input type="checkbox" class="mySelector" value="tickOne" />
<input type="checkbox" class="mySelector" value="tickTwo" />
....
<input type="hidden" id="myHiddenInput" />

You can give it a try here

Nick Craver
so I should create a hidden input field and then once the user has selected the tick boxes they will be assigned to the hidden input fields value?
@kielie - Yup, if you want to do it client-side something like that...I added a demo/example markup to better illustrate it :)
Nick Craver
Thanx a million! ;)
How can I limit the amount of boxes ticked to 3?
@kielie - add a :lt(3) after the :checked (no spaces) to get the first 3 only :)
Nick Craver
Thanx that worked, is there a way to disable the rest of the check boxes once the limit is reached? sorry to be a bother!