这是我的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);
});
});
这是我的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>
在这个JavaScript功能中,我可以获得成人和儿童的总数。我想分别得到成年人和没有孩子。如果有人能帮助我,我将非常感激。
答案 0 :(得分:1)
你这样做是错误的。试试 -
var total = 0;
$('#roomOptions select').each(function() {
total += parseInt($(this).val());
});
在您的代码中 -
$(document).ready(function() {
$('#roomOptions select').change(function() {
var total = 0;
$('#roomOptions select').each(function() {
total+=parseInt($(this).val());
});
$('#roomOptions #roomOptions_total').html(total);
});
});
答案 1 :(得分:1)
试试这个:
$(document).ready(function () {
$('#roomOptions select').change(function () {
var total = 0;
$('#roomOptions option').each(function () {
total += parseInt($(this).val());
});
$('#roomOptions #roomOptions_total').html(total);
});
});
答案 2 :(得分:0)
使用
$('#roomOptions #select').each(function() {});
$('#roomOptions #select2').each(function() {});