简体   繁体   English

如何在生成的html的onclick函数中传递参数

[英]How to pass parameters in onclick function of generated html

In my controller code contain html code in appened form.When i pass the parameters to the onclick function, I didn't get the parameters in the corresponding function. 在我的控制器代码中包含附加形式的html代码。当我将参数传递给onclick函数时,我没有在相应函数中获取参数。

controller 调节器

         foreach ($cart as $item){ 
          $row_id = $item['rowid'];
          //    $count++;
            $output.='
            <tr>
               <td>'.$item['name'].'</td>
                <td>'.$item['price'].'</td>
               <td>'.$item['qty'].'</td>
               <td>'.number_format($item['subtotal'],2).'</td>
               <td> <a href="" onclick="remove("'.$row_id.'")" class="item-remove"><i class="zmdi zmdi-close"></i></a></td>
            </tr>

            ';

     }

script 脚本

   function remove(row_id)
    {

        alert(row_id);
    }

Onclick function remove(), alert is not working Onclick函数remove(),警报不起作用

your old code is producing 您的旧代码正在产生

<td> <a href="" onclick="remove("1")" class="item-remove"><i class="zmdi zmdi-close"></i></a></td>

which is an incorrect HTML 这是不正确的HTML

just replace this 只需替换这个

onclick="remove("'.$row_id.'")"

with this 有了这个

onclick="remove(\''.$row_id.'\')"

see a demo : https://eval.in/830107 看到一个演示: https : //eval.in/830107

onclick="remove("'.$row_id.'")"

Will result in: 将导致:

onclick="remove("123")"

See where its going wrong? 看看哪里出了问题? Onclick now contains only the portion remove(, because than a double quote termintes the onclick content. Onclick现在仅包含remove()部分,因为双引号引起了onclick内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM