我的数据库中有一个字符串值 id ,我有一个带有javascript函数的按钮 onclick ="值('参数' )"
我的问题是当我试图点击该按钮时出现此错误:
(index):1 Uncaught ReferenceError: argument is not defined
我知道这是因为参数不被视为字符串。
如何将参数作为字符串传递?
所有这些都是一个包含在php函数中的表,它显示了我数据库中的信息
这是我的完整代码。
<?php
function oportunities($resultQuery)
{
echo '<div class="table-responsive">
<table class="responstable">
<thead class="thead">
<tr>
<th>No.</th>
<th>value1</th>
<th>value2</th>
<th>value3</th>
<th>value4</th>
<th>value5</th>
<th>value6</th>
<th>value7</th>
<th>value8</th>
<th>value9</th>
<th>value10</th>
<th>value11</th>
<th>value12</th>
<th>value13</th>
<th>value14</th>
<th>value15</th>
</tr>
</thead>
<tbody>';
foreach($resultQuery as $row){
echo '<tr>
<td> '. $row['id'] .'</td>
<td>'. $row['value1'] .'</td>
<td>'. $row['value2'] .'</td>
<td class="descripcion">
<p class="text-descripcion">'.$row['value3'].' </p>
</td>
<td> '.$row['value4'].' </td>
<td> '. $row['value5'].' </td>
<td><p class="text-center">'. $row['value6'] .'% </p></td>
<td> '.$row['value7'].' </td>
<td> '.$row['value8'].' </td>
<td> '.$row['value9'].' </td>
<td> '.$row['value10'].'</td>
<td> '.$row['value11'].' </td>
<td> '.$row['value12'].' </td>
<td> '. $row['value13'].' </td>
<td>
<button class="center-block btn btn-acept"><i class="fa fa-check" aria-hidden="true"></i>
</button>
</td>
<td>
<button onclick="values('. $row['id']. ')" class="center-block btn btn-editar launch-modal" data-modal-id="modal-edit"><i
class="fa fa-pencil" aria-hidden="true"></i>
</button>
</td>
</tr>';
}
echo '</tbody>
</table>
</div>';
}
答案 0 :(得分:2)
添加引号,然后转义它们
<button onclick="values(\''. $row['id']. '\')" class="center-block
当您使用双引号作为属性时,您需要单引号用于参数,但您已经在PHP中使用单引号,因此您必须转义它们,以便它们不会被PHP解析。
当然,更好的选择是根本不使用内联javascript,而是addEventListener
。