lundi 29 août 2016

jQuery, getting the array of values corresponding to selected checkboxes

<form id="myform">
<table>         
<tbody>
    <tr>
    <td class="textcenter">
        <input type="checkbox" name="domains[]" value="mydomainname.com" checked=""> 
        <input type="hidden" name="action[]" value="buy">
    </td>
    <td>mydomainname.com</td>
    <td >Available! Choose the number of years</td>
    <td class="textcenter">
        <select class="leftmargin10 bottommargin10 form-control" name="period[]">
            <option value="1">1 Year/s @ $24.99</option>
            <option value="2">2 Year/s @ $49.98</option>
            <option value="3">3 Year/s @ $74.97</option>
        </select>
    </td>
    </tr> 
    <tr>
    <td class="textcenter">
        <input type="checkbox" name="domains[]" value="mydomainname.net"> 
        <input type="hidden" name="action[]" value="transfer">
    </td>
    <td>mydomainname.net</td>
    <td>Available! Choose the number of years</td>
    <td class="textcenter">
        <select class="leftmargin10 bottommargin10 form-control" name="period[]">
            <option value="1">1 Year/s @ $24.99</option>
            <option value="2">2 Year/s @ $49.98</option>
            <option value="3">3 Year/s @ $74.97</option>
        </select>
    </td></tr> 
</tbody></table>

<button type="submit" name="submitButton" class="btn-yellow" value="Buy">Register Now!</button>
</form>

There'll be 10-15 rows like above.

On the jQuery side I have:

$("#domainform").submit(function(){
        event.preventDefault();
        var domains= $('input[name="domains[]"]:checked').map(function(){
                return $(this).val();
            }).get();

        var action= $('input[name="action[]"]').map(function(){
            return $(this).val();
            }).get();

        var period= $('select[name="period[]"]').map(function(){
            return $(this).val();
            }).get();
});

This code currently gives me the correct checkbox values but it gives the values for all the dropdowns (regardless of checkbox statuses). How do I tie these two together and get only the values for the dropdowns corresponding to the checked checkboxes.

Thank you!




Aucun commentaire:

Enregistrer un commentaire