I've been digging around to try and get a list of comma separated values from a set of checkboxes. There have been lots of useful answers here, but I can't work out a final kink. My code is behaving oddly depending on whether or not the first checkbox is checked and I can't work out why (this is my first ever attempt at javascript!)
<script type="text/javascript">
function updateTextArea() {
var allVals = $('input.gift:checked').map(
function() {return this.value;}).get().join();
$('#txtValue').val(allVals)
}
$(function () {
$('#gift input').click(updateTextArea);
updateTextArea();
});
</script>
And here's the html that accompanies it.
<fieldset id="gift">
<input type='checkbox' name='lFreeProductid' value='8840' id="lFreeProductid1" class="gift"></td>
<td class="style42">64 SSG </td>
<td class="style32">
</td>
</tr>
<tr id="tr2">
<td class="style33">
<input type='checkbox' name='lFreeProductid' value='420' id="lFreeProductid1" class="gift"></td>
<td class="style41"> Perfect D </td>
<td>
</td>
</tr>
<tr id="tr3">
<td class="style33">
<input type='checkbox' name='lFreeProductid' value='460' id="lFreeProductid1" class="gift"></td>
<td class="style41">Soccer Attack</td>
<td>
</td>
</tr></fieldset>
<tr>
<td class="style33">
</td>
<td class="style41">
</td>
<td>
</td>
</tr>
</table>
<textarea id="txtValue"></textarea>
I know this is the sort of question that's been asked a thousand times, but I can't seem to find a solution to this specific issue. Any help would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire