1 to 4 of 4
So, I have a form that looks something like the one below. Note that the form user can add table rows dynamically but for simplicity's sake I'm showing three existing rows. Normally there'd be one row to start and then new ones could be added.
<form>
<table>
<tr id="row-1">
<td>
<select id="select-1">
<option value="10">Ten</option>
<option value-"11">Eleven</option>
</select>
</td>
</tr>
<tr id="row-2">
<td>
<select id="select-2">
<option value="10">Ten</option>
<option value-"11">Eleven</option>
</select>
</td>
</tr>
<tr id="row-3">
<td>
<select id="select-3">
<option value="10">Ten</option>
<option value-"11">Eleven</option>
</select>
</td>
</tr>
</table>
<input type="hidden" name="all-select-values" id="all-select-values" value="" />
<input type="submit" value="Submit" />
</form>
What I need to happen is when the form user selects options from the dropdowns in each table row, the value is added to a comma-delimited value in the hidden input "all-select-values". That value would then be sent when the form is clicked.
So far I've got this which works for just one dropdown but not multiple.
$(document).ready(function($) {
var select-values = [];
$("select[id^=select-]").change(function(e){
select-values.push( $(this).val() );
$("#all-select-values").val( select-values.join(',') );
});
});
Any ideas?
function setHandlers(e) {
$('select[id^=select-]').change(updateValue);
}
function updateValue() {
var select_values = [];
$('select[id^=select-]').each(function(e) {
if ($(this).val()) select_values.push($(this).val());
});
$('#all-select-values').val(select_values.join(','));
}
$(document).ready(function() {
setHandlers();
updateValue();
});if ($(this).val()) { … } check so that the function would work if also using multiple="multiple" select elements (which don't have a default selected option), without adding redundant commas/delimiters to the value of the hidden input.
Works a treat. Thanks for the tips too.
1 to 4 of 4