Jquery 总计
好吧,挑战相对较小,但它是一只在我头上嗡嗡作响的苍蝇。 在jQuery中查看$(“$”+table\u-number).text(“$”+grand\u-total[table\u-number].toFixed(2)) 我尝试做的是grand_total[表号]+=sub_total;但我一直在找NaN CSSJquery 总计,jquery,arrays,codeigniter,function,variables,Jquery,Arrays,Codeigniter,Function,Variables,好吧,挑战相对较小,但它是一只在我头上嗡嗡作响的苍蝇。 在jQuery中查看$(“$”+table\u-number).text(“$”+grand\u-total[table\u-number].toFixed(2)) 我尝试做的是grand_total[表号]+=sub_total;但我一直在找NaN CSS .guest_table { float:left; position: relative; margin-left: 30px; wi
.guest_table
{
float:left;
position: relative;
margin-left: 30px;
width: 280px;
}
h1
{
text-align: center;
}
table
{
margin-left: auto;
margin-right: auto;
width: 425px;
}
table, tr
{
border: 5px solid black;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */
}
tr
{
height: 65px;
}
.submit
{
text-align: center;
}
.input
{
border: none;
background: #fff url(../images/rounded-corner.png) no-repeat left top;
background-size:185px 37px;
width: 160px;
padding: 10px;
}
.input:focus
{
background-color: #ffff33 ;
}
#submit_form
{
border: 50px;
padding: 10px;
margin-left: auto;
margin-right: auto;
}
.error
{
text-align: center;
color: red;
font-size: 20px;
margin-bottom: 20px;
background-color: yellow;
width: 25%;
margin-left: auto;
margin-right: auto;
}
.list_total
{
display: inline-block;
padding-left: 5px;
}
#option_list
{
width: 25%;
}
#already_paid
{
text-align: center;
}
input[type=submit]:hover,input[type=reset]:hover
{
background-color: #8F5B00;
color: white;
}
button, input[type=submit]
{
margin-left: 10px;
}
button:hover
{
background-color: #8F5B00;
color: white;
}
代码点火器视图
<?php echo validation_errors(); ?>
<?php
$total_guests = count($results);
$attributes = array(
'id' => 'submit_form'
);
echo form_open('main/form_validation/options');
?>
<div id="center">
<?php
foreach ($results as $result) {
?>
<table class="guest_table" id="guest_<?php echo $result->id; ?>">
<th>
<?php echo $result->full_name; ?>
</th>
<tr id="option_list">
<td>
<?php
$b = $price2 > 0 ? " $" . $price2 : "N/A";
$day = array(
array(
'name' => 'friday_performance_' . $result->id,
'id' => 'friday_performance' . $result->id,
'value' => 'friday_performance'
),
array(
'name' => 'friday_banquet_' . $result->id,
'id' => 'friday_banquet' . $result->id,
'value' => 'friday_banquet'
),
array(
'name' => 'saturday_' . $result->id,
'id' => 'saturday' . $result->id,
'value' => 'saturday'
),
);
$meal_pref = array(
array(
'name' => 'meal_' . $result->id,
'id' => 'chicken',
'value' => 'chicken'
),
array(
'name' => 'meal_' . $result->id,
'id' => 'fish',
'value' => 'fish'
),
array(
'name' => 'meal_' . $result->id,
'id' => 'vegetarian',
'value' => 'vegetarian'
),
);
echo form_checkbox($day[0]);
?>
Friday Performance <?php echo " $" . $price1; ?><br>
<?php echo form_checkbox($day[1]);
?> Friday Banquet <?php echo $b; ?><br>
<div class="meal_choice" id='meal_choice<?php echo $result->id; ? >'>
<?php
echo form_radio($meal_pref[0]);
?> Chicken <br>
<?php echo form_radio($meal_pref[1]);
?> Fish <br>
<?php echo form_radio($meal_pref[2]);
?> Vegetarian<br>
</div>
<?php echo form_checkbox($day[2]); ?> Saturday Workshop w/lunch <?php echo " $" . $price3; ?><br>
<br><br>
</td>
</tr>
<tr>
<td>
Total:<div class="list_total" id="list_total<?php echo $result->id; ?>"></div>
</td>
</table>
<?php } ?>
</div>
<div class="submit">
<?php
echo ("<input type='hidden' name='email' value=" . $this->session->userdata('email') . ">");
$data = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Complete Form',
'type' => 'submit'
);
echo form_submit($data);
echo form_button('exit', 'Exit');
?>
</div>
<?php
echo form_close();
?>
您可以很容易地调试它
在浏览器中打开控制台(F12)
键入以下内容:
table_number
sub_total
grand_total[table_number]
这会告诉你你需要什么——一不是数字。如有必要,在控制台中编辑您的问题,提及上述变量的值,我们将进一步提供帮助
在浏览器中舒适地使用控制台将有助于开发客户端web应用。您已经初始化了grand\u total
变量,但您也应该在开始添加任何内容之前初始化grand\u total[表号]
即索引值
考虑修改以下行:
$(':checkbox').change(function(){
switch($(this).attr('id'))
{
为此:
$(':checkbox').change(function(){
if (typeof (grand_total[table_number]) === "undefined") {
grand_total[table_number] = 0;
}
switch($(this).attr('id'))
{
您需要确保与+
一起使用的所有变量都应该是相同的类型。如果您试图添加整数,请确保使用parseInt
方法,如果您使用的是string,则使用toFixed
。谢谢…我与firebug进行了双重检查…显示表号是一个数字(数字周围没有引号)以及子总数。抱歉,上一条评论超时…无论如何。。。谢谢…我仔细检查了firebug…显示表号是一个数字(数字周围没有引号)以及子总数。奇怪的是,当我将鼠标移到grand_total上时,我得到[0,NaN]甚至认为grand_total[表号]+=sub_total;
$(':checkbox').change(function(){
if (typeof (grand_total[table_number]) === "undefined") {
grand_total[table_number] = 0;
}
switch($(this).attr('id'))
{