使用Javascript打印HTML:未定义错误


Printing HTML with Javascript : is not defined error

我正在开发一个使用HTML、PHP和Javascript JQuery的网站我有一个html格式的数据表,我在其中打印数据库中的结果。每一行都有一个删除按钮,我在其中调用一个javascript函数。

现在我想使用javascript在该表中插入一行,这很有效,但问题是如何正确打印删除按钮的函数。

onclick函数需要解析元素和品牌名称(关于汽车)

代码将解释更多:

数据表:

<table class="table table-striped table-bordered table-hover roles-table" id="makes">
                                <thead>
                                <tr>
                                    <th>Merk</th>
                                    <th>Verwijderen</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php $result = $userProfile->getSubBrands($companyId);
                                foreach($result as $subBrand):
                                    ?>
                                    <tr class="role-row" >
                                        <td>
                                            <?php echo htmlentities($subBrand['brand_name']); ?>
                                        </td>
                                        <td>
                                            <button type="button" class="btn btn-danger" onclick="profile.deleteSubs(this,'<?php echo htmlentities($subBrand['brand_name']); ?>');">
                                                <i class="fa fa-trash-o"></i> Verwijderen </button>
                                        </td>
                                    </tr>
                                <?php
                                endforeach;
                                ?>
                                </tbody>
                            </table>

如何使用jquery添加行(res来自JSON结果解析):

t.row.add([
                res.brandName,
                '<button type="button" class="btn btn-danger" onclick="profile.deleteSubs(this,'+res.brandName+');"><i class="fa fa-trash-o"></i> Verwijderen</button>'
            ]).draw();

这添加了行,但当我单击按钮时,我会得到错误"Uncaught ReferenceError:宝马没有定义"宝马是品牌的名称

有什么想法吗?

试试这个:

t.row.add([
                res.brandName,
                '<button type="button" class="btn btn-danger" onclick="profile.deleteSubs(this,'''+res.brandName+''');"><i class="fa fa-trash-o"></i> Verwijderen</button>'
            ]).draw();