mercredi 14 septembre 2016

Comma separated list from checkbox values

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">&nbsp;
                    </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>&nbsp;
                    </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>&nbsp;
                    </td>
                </tr></fieldset>

                <tr>
                    <td class="style33">&nbsp;
                    </td>
                    <td class="style41">&nbsp;
                    </td>
                    <td>&nbsp;
                    </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