• 1.1k
  • 1.3k
  • 193.9k

sum Operation with select option doesnt work

Aug 3 2022 2:00 PM


I have a trouble. I have select option with value. I need to make sum of each of ths selct option. But when you select nothing happen.

$(document).ready(function() {
    $("#products select").on("change", function() {
        var id = this.value;
            url: "ajax.json",
            method: "get",
            data: "id=" + id,
            success: function(response) {               

         //initialize totalPrice to 0 if doesn't contain a value
         var totalPrice = $("#totalprice").val() !== "" ? parseInt($("#totalprice").val()): 0;

                var datashow =  response;//JSON.parse(response);
                var price1 = parseInt(datashow[0].price);
                /*var time1 = datashow[0].time;*/

                //add the total price
                $("input#totalprice").val(totalPrice + price1);

                //Do something similar for time, I don't how you plan on parsing this?


<div id="products">
<select id="product1" name="product1">
    <option value="" selected="selected">-</option>
    <option value='1'>1</option>
<select id="product2" name="product2">
    <option value="" selected="selected">-</option>
    <option value='2'>2</option>
<select id="product3" name="product3">
    <option value="" selected="selected">-</option>
    <option value='3'>3</option>
<select id="product4" name="product4">
    <option value="" selected="selected">-</option>
    <option value='4'>4</option>
<select id="product5" name="product5">
    <option value="" selected="selected">-</option>
    <option value='5'>5</option>

<input id="totalprice" type="text" name="totalprice" />
<!--input id="totaltime" type="text" name="totaltime" /-->


Answers (1)