简体   繁体   English

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

[英]Printing HTML with Javascript : is not defined error

I'm working on a website using HTML, PHP and Javascript JQuery I have a datatable in html where I print my results from a database. 我正在使用HTML,PHP和Javascript JQuery的网站上工作。我有html中的数据表,可在其中从数据库中打印结果。 Each row has a delete button, where I call a javascript function. 每行都有一个删除按钮,在这里我称为javascript函数。

Now I want to insert a row in that table using javascript, this works, but the problem is how to print the function for the delete button correctly. 现在,我想使用javascript在该表中插入一行,这行得通,但是问题是如何正确打印删除按钮的功能。

The onclick funtion needs to parse the element and the brand name (it's about cars) onclick函数需要解析元素和品牌名称(与汽车有关)

The code will explain more: 该代码将解释更多:

Datatable : 数据表:

<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>

How I add my row with jquery (the res comes from JSON result parsing) : 我如何用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();

This adds the row, but when I click the button I get the error "Uncaught ReferenceError: BMW is not defined" BMW is the name of the brand 这将添加行,但是当我单击按钮时,出现错误“ Uncaught ReferenceError:未定义BMW” BMW是品牌的名称

Any ideas? 有任何想法吗?

Try this: 尝试这个:

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();

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

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