如何获得下拉列表值的总和?

时间:2022-03-06 23:07:01

This is my JavaScript function:

这是我的JavaScript函数:

<script type="text/javascript">


$(document).ready(function() {
    $('#roomOptions select').change(function() {
        var total = 0;
        $('#roomOptions select').each(function() {

        });(function() {
            total+=parseInt($(this).val());

        });
        $('#roomOptions #roomOptions_total').html(total);
    });
});

This is my php dropdown list code.

这是我的php下拉列表代码。

<select name="select" id="select">
     <option value="0">Select adults</option>       
       <?php 
       $result = mysql_query("select max_adult from room_category where  room_type = '".$var_value."' ");
       while ($data = mysql_fetch_array($result))
       {
           $adult = $data['max_adult']; 

           for($n=1; $n<=$adult; $n++)
           {


            ?>
           <option value="<?php echo $n ?>"><?php echo $n ?> </option>
      <?php     } ?>

           <?php
           }
         ?>
         </select></td>
  <td align="center">  <select name="select2" id="select2">
     <option value="0">Select children</option>       

    <?php 
       $result1 = mysql_query("select max_child from room_category where  room_type = '".$var_value."' ");
       while ($data1 = mysql_fetch_array($result1))
       {
           $child = $data1['max_child']; 

           for($k=1; $k<=$child; $k++)
           {


            ?>
    <option value="<?php echo $k ?>"><?php echo $k ?> </option>
    <?php     } ?>

    <?php
           }
         ?>
  </select></td>

</tr> <?php } ?>

<tr> 

In this JavaScript function i can get total number of adults and children. I want to get No of adults and No of children separately. I would be very grateful if any one can help me.

在这个JavaScript函数中,我可以获得成人和儿童的总数。我想分别得到成年人和没有孩子。如果有人能帮助我,我将非常感激。

3 个解决方案

#1


1  

You are doing it in the wrong way. Try with -

你是以错误的方式做到的。尝试 -

        var total = 0;
        $('#roomOptions select').each(function() {
              total += parseInt($(this).val());
        });

In your code -

在你的代码中 -

$(document).ready(function() {
    $('#roomOptions select').change(function() {
        var total = 0;
        $('#roomOptions select').each(function() {

            total+=parseInt($(this).val());

        });
        $('#roomOptions #roomOptions_total').html(total);
    });
});

#2


1  

Try this:

$(document).ready(function () {
    $('#roomOptions select').change(function () {
        var total = 0;
        $('#roomOptions option').each(function () {
            total += parseInt($(this).val());
        });
        $('#roomOptions #roomOptions_total').html(total);
    });
});

#3


0  

Use

$('#roomOptions #select').each(function() {});
$('#roomOptions #select2').each(function() {});

#1


1  

You are doing it in the wrong way. Try with -

你是以错误的方式做到的。尝试 -

        var total = 0;
        $('#roomOptions select').each(function() {
              total += parseInt($(this).val());
        });

In your code -

在你的代码中 -

$(document).ready(function() {
    $('#roomOptions select').change(function() {
        var total = 0;
        $('#roomOptions select').each(function() {

            total+=parseInt($(this).val());

        });
        $('#roomOptions #roomOptions_total').html(total);
    });
});

#2


1  

Try this:

$(document).ready(function () {
    $('#roomOptions select').change(function () {
        var total = 0;
        $('#roomOptions option').each(function () {
            total += parseInt($(this).val());
        });
        $('#roomOptions #roomOptions_total').html(total);
    });
});

#3


0  

Use

$('#roomOptions #select').each(function() {});
$('#roomOptions #select2').each(function() {});