mercredi 20 janvier 2021

Build list of values corresponding to checkbox node.js

I have a HTML form where users can select items using the checkbox and enter the amount of each item that they want. I want to get the amount that the user entered but only for the items with the checkbox selected. I'm trying to create a list of amount values that correspond to the selected checkboxes but I'm not sure if I'm doing it correctly. How do I retrieve the amount values from the list in node.js?

<form method="POST" action="/topic7/mid-term/calculate">
        <table style="width:100%">
                <tr>
                        <th>Selected</th>
                        <th>Amount</th>
                        <th>Name</th>
                        <th>Typical values</th>
                        <th>Unit of typical values</th>
                        <th>Calories</th>
                        <th>Carbs</th>
                        <th>Fat</th>
                        <th>Protein</th>
                        <th>Salt</th>  
                        <th>Sugar</th>
                </tr>
 
                <% availableFood.forEach(function(food_item){ %>
                <tr>
                        <td><input type="checkbox" name="checkbox[]" value= "<%= food_item.name %>"></td>    
                        <td><input type="text" name="amt" value= "1" style="width: 30px;"></td>
                        <td><%= food_item.name %></td>
                        <td><%= food_item.typical_values %></td> 
                        <td><%= food_item.unit_of_the_typical_value %></td>
                        <td><%= food_item.calories %></td>
                        <td><%= food_item.carbs %></td>
                        <td><%= food_item.fat %></td>
                        <td><%= food_item.protein %></td> 
                        <td><%= food_item.salt %></td> 
                        <td><%= food_item.sugar %></td>
                </tr>
                <% }) %>                     
        </table>
        <p><input type="submit" value="Calculate sum" onclick='buildlist("checkbox","amt");'/></p>

        <script>
                function buildlist(listName, labelName) {
                        var checkbox = document.getElementsByName(listName);
                        var amount = document.getElementsByName(labelName);
                        amount.value = '';
                        for(var i = 0; i < checkbox.length; i++){
                                amount.value += checkbox[i].value.toString() + ',';
                        }
                }
        </script>
</form>



Aucun commentaire:

Enregistrer un commentaire