最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass parameters in onclick function of generated html - Stack Overflow

programmeradmin1浏览0评论

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.

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

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.

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

Share Improve this question edited Jul 10, 2017 at 13:06 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jul 10, 2017 at 10:57 robinsrobins 1,6687 gold badges34 silver badges71 bronze badges 5
  • why you are not using jquery..? – Dharmendra Singh Commented Jul 10, 2017 at 11:03
  • Try changing remove("'.$row_id.'") to remove('.$row_id.'), that should work. – Milan Chheda Commented Jul 10, 2017 at 11:04
  • its not working – robins Commented Jul 10, 2017 at 11:06
  • if you use remove(2) it works..? – Masum billah Commented Jul 10, 2017 at 11:08
  • @DharmendraSingh You've just settled a bet :P – Flosculus Commented Jul 10, 2017 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 5

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

just replace this

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

with this

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

see a demo : 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.

发布评论

评论列表(0)

  1. 暂无评论