합계

2013-01-08 2 views
0

좋아요, 도전은 비교적 작지만 머리 주위를 날아 다니는 날입니다. $ ("# list_total"+ table_number) .text ("$"+ grand_total [table_number] .toFixed (2))에서 jQuery를 살펴 보자.합계

제가 시도한 것은 grand_total [table_number] + = sub_total입니다. 하지만 계속 NaN.

CSS

.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; 
    } 

CodeIgniter를보기

<?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]); 
        ?>&nbsp;&nbsp; 
        Friday Performance <?php echo " $" . $price1; ?><br> 

        <?php echo form_checkbox($day[1]); 
        ?>&nbsp;&nbsp;Friday Banquet <?php echo $b; ?><br> 
        <div class="meal_choice" id='meal_choice<?php echo $result->id; ? >'> 
         <?php 
         echo form_radio($meal_pref[0]); 
         ?>&nbsp;&nbsp;Chicken <br> 
         <?php echo form_radio($meal_pref[1]); 
         ?>&nbsp;&nbsp;Fish <br> 
         <?php echo form_radio($meal_pref[2]); 
         ?>&nbsp;&nbsp;Vegetarian<br> 

        </div> 
    <?php echo form_checkbox($day[2]); ?>&nbsp;&nbsp;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(); 
?> 

jQuery를이

<script> 
$(document).ready(function(){ 

    $(".list_total").text("$0.00"); 
    $(".meal_choice").hide(); 
    var total_attending = (<?php echo $total_guests; ?> + 1); 

    $("[name='exit']").click(function(){ 
     window.location.href='<?php echo base_url(); ?>/main/logout'; 
    }); 

    var table_number = 0; 
    $('.guest_table').click(function(){ 
     var table_id = $(this).attr('id'); 
     table_number = parseInt(table_id.charAt(table_id.length-1)); 
    }); 
    var grand_total=[]; 
    grand_total[table_number]=0.00; 

    $(':checkbox').change(function(){ 

     switch($(this).attr('id')) 
     { 
      case 'friday_performance' + table_number: 
       var sub_total= 0.00; 
       sub_total = parseInt(friday_performance()); 
       grand_total[table_number] += sub_total; 

       $("#list_total"+table_number).text("$"+ grand_total[table_number].toFixed(2)); 
       break; 

      case 'friday_banquet' + table_number: 
       var sub_total= 0.00; 
       sub_total = parseInt(friday_banquet()); 
       grand_total[table_number] += sub_total; 
       $("#list_total"+table_number).text("$"+ grand_total[table_number].toFixed(2)); 
       break; 
      case 'saturday' + table_number: 
       var sub_total= 0.00; 
       sub_total += parseInt(saturday()); 
       grand_total[table_number] = sub_total; 
       $("#list_total"+table_number).text("$"+ grand_total[table_number].toFixed(2)); 
       break; 

     } 
    }); 

    function friday_performance() 
    { 
     if ($("#friday_performance"+table_number).is(":checked")) 
     { 
      var price1 = <?php echo $price1; ?>; 
      return price1; 
     }else if ($("#friday_performance"+table_number).not(":checked")) 
     { 
      var price1 = 0; 
      return price1; 
     } 
    } 

    function friday_banquet() 
    { 
     if ($("#friday_banquet"+table_number).is(":checked")) 
     { 
      $("#meal_choice"+table_number).show("slow"); 
      var price2 = <?php echo $price2; ?>; 
      return price2; 
     }else if ($("#friday_banquet"+table_number).not(":checked")) 
     { 
      $("#meal_choice"+table_number).hide("slow"); 
      $('input:radio').prop("checked",false); 
      var price2 = 0; 
      return price2; 
     } 
    } 

    function saturday() 
    { 
     if ($("#saturday"+table_number).is(":checked")) 
     { 
      var price3 = <?php echo $price3; ?>; 
      return price3; 
     }else if ($("#saturday"+table_number).not(":checked")) 
     { 
      var price3 = 0; 
      return price3; 
     } 

    } 

    $("#submit").click(function(){ 
     alert("Got it!"); 
    }); 
}); 
</script> 
+0

당신이 동일한 유형이어야한다'+'로 사용하고 있는지 확인 모든 변수를 확인해야합니다. 정수를 추가하려고한다면'parseInt' 메쏘드를 사용하고 string을 사용한다면'toFixed'를 사용하십시오. – Amyth

답변

1

당신은이 전자를 디버깅 할 수 있습니다 asily.

브라우저 (F12)에서 콘솔을 엽니 다. 다음

유형 : 당신이 필요로하는 무엇을 말할 것이다

table_number 

sub_total 

grand_total[table_number] 

- 하나는 번호가 아닙니다. 필요한 경우 콘솔에서 위 변수의 값을 언급하는 질문을 편집하면 더 도움이 될 것입니다.

브라우저에서 콘솔을 편안하게 사용하면 클라이언트 측 웹 앱을 개발하는 데 도움이됩니다.

+0

고마워 ... 나는 firebug로 두 번 확인했다 ... table_number는 숫자 (숫자를 따옴표없이)와 sub_total을 보여준다. – Softwaretech

+0

죄송합니다, 이전 의견에 시간 초과되었습니다 ... 어쨌든 ... 고마워 ... 나는 방화범과 함께 두 번 확인했습니다 ... table_number는 숫자 (숫자를 따옴표없이)와 sub_total을 보여줍니다. 이상한 점은, mouseover grand_total 일 때 ... [0, NaN]도 grand_total [table_number] + = sub_total이라고 생각하는 것입니다. <--- sub_total이 값을 표시하고 있습니다. – Softwaretech

1

grand_total 변수를 초기화했지만 아무 것도 추가하기 전에 색인을 사용하여 grand_total[table_number] 즉 값을 초기화해야합니다.

는 다음 행을 수정하십시오 :

$(':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')) 
    { 
+0

감사합니다 .... 절대 정의해서는 안됩니다. – Softwaretech

+0

나에게 전화해라. ... 만일 당신이 벌써 가지지 않고 있으면. .. 그 트릭을했다! 감사! – Softwaretech