dpq39825 2016-07-27 15:57
浏览 67
已采纳

Javascript - 将动态递归PHP值作为javascript函数传递

New to javascript. I have a while loop in PHP that looks like this

while ($row_voucher=mysql_fetch_row($risultato_query_voucher)) {    //INFO poche righe sotto, date_format da solo non funziona 
        echo "<tr>

                <td id=" . $row_voucher[3] ."><input type='checkbox' value='" . $row_voucher[3] . "' onchange='ControllaCodice(this)' id='" . $row_voucher[6] . "'></td>

                <td>" . $row_voucher[2] . "</td>

                <td>" . $row_voucher[3] . "</td>

                <td>" . date_format( new DateTime($row_voucher[4]), 'd/m/Y' ). "</td>

                <td>" . date_format( new DateTime($row_voucher[5]), 'd/m/Y' ). "</td>

                <td>" . $row_voucher[6] . "</td>

              </tr>
";
      }

What i need to do is, when you click on the checkbox, to take the value of the first input type and add it. Since it's a while loop this will generate a table with a lot of checkboxes each one with a different value. So if you for example check 3 checkboxes the javascript I've written should take the 3 values and add it and then print it. If possible i'd need also to add the value on check and subtract it on uncheck.

Here's the not working javascript code:

<script type='text/javascript'>
   function ControllaCodice(this.value){
   window.alert(this.value);
   }
 </script>";

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • douchan0523 2016-07-27 16:00
    关注

    You don't pass this.value, but this to a function which accepts one attribute. The name of the variable doesn't matter, only the contents does. Though, you can't accept this in the function, it's a reserved variable.

    Also, it's not obligatory to use window.alert, could be done just with alert.

    So, the code is:

    ... onchange = "ControllaCodice(this)" ...
    

    function ControllaCodice(c){
       alert(c.value);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题