简体   繁体   English

如何防止 JavaScript 中的引号错误?

[英]How to prevent error with quotation marks in JavaScript?

I´m sending my data through a JavaScript like this:我正在通过这样的 JavaScript 发送我的数据:

function: enviaEditar('.$row->id.',\' '.$row->nombre.' \')'

As you can see I'm using /' in order to seen a string (text) but I have an error if the value of $row->nombre contain ' or ". Is there a way to prevent both of them.正如您所看到的,我正在使用 /' 来查看字符串(文本),但是如果 $row->nombre 的值包含 ' 或 ",我会出错。有没有办法阻止它们。

while($row = $result->fetch_object()) {
    echo '
    <tr style="font-size:12px">
        <td>'.$row->nombre.'</td>
        <td style="text-align: center;">
            <button type="button" data-toggle="modal" data-target=".ModalEditar" onclick="enviaEditar('.$row->id.',\''.$row->nombre.'\');">
            </button>
        </td>
    </tr>';
}

You need to url encode the PHP code.您需要对 PHP 代码进行 url 编码。 Change the line to this:将行更改为:

<button type="button" data-toggle="modal" data-target=".ModalEditar" onclick="enviaEditar('.urlencode( $row->id ) .',\''. urlencode( $row->nombre ) .'\');">

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

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