Inserting multiple tick boxes into one database field
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 cho开发者_开发百科oses 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!
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
精彩评论