Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript onclick-不正确的解释_Javascript_Jquery_Html - Fatal编程技术网

JavaScript onclick-不正确的解释

JavaScript onclick-不正确的解释,javascript,jquery,html,Javascript,Jquery,Html,将onclick事件存储到页面的html代码时出现问题。 当表格充满行时,tr上的产品名称应如下所示: <tr onclick="cart_pr('0K2FA-50030XX')">...</tr> <tr onclick="cart_pr('1000" fa')>...</tr> function FillArticles() { //--------- Request to DB -----------// //---

将onclick事件存储到页面的html代码时出现问题。 当表格充满行时,
tr
上的产品名称应如下所示:

<tr onclick="cart_pr('0K2FA-50030XX')">...</tr>
<tr onclick="cart_pr('1000" fa')>...</tr>
function FillArticles()
{
    //---------    Request to DB -----------//
    //---------    Parse json etc.

    for(i=0;i<json.length;i++)
    {
        //-----    Here goes loop to fill a table  ----//

        res.push("<tr onclick=cart_pr('"+obj.name+"')></tr>");

        //-----    I don't show more, cause only line above is important, so....
        //-----    below Closes the row with </tr>
        res.push("</tr>");
    }
    $("#mytable").html(res.join(''));
    //---------    In this line in debugger, res is absolutely OK
}
。。。
所有文章中只有一篇(名为“1000 fa”)错误地显示如下:

<tr onclick="cart_pr('0K2FA-50030XX')">...</tr>
<tr onclick="cart_pr('1000" fa')>...</tr>
function FillArticles()
{
    //---------    Request to DB -----------//
    //---------    Parse json etc.

    for(i=0;i<json.length;i++)
    {
        //-----    Here goes loop to fill a table  ----//

        res.push("<tr onclick=cart_pr('"+obj.name+"')></tr>");

        //-----    I don't show more, cause only line above is important, so....
        //-----    below Closes the row with </tr>
        res.push("</tr>");
    }
    $("#mytable").html(res.join(''));
    //---------    In this line in debugger, res is absolutely OK
}

我需要添加引号
&39而不是

res.push("<tr onclick='cart_pr(&#39;"+obj.name+"&#39;);'></tr>
res.push(“
但这肯定不是制作具有可单击行的表的最佳方法,特别是在使用具有多个参数的函数时


最好在JQuery中使用
.on()
而不是这种方法。

obj.name
中使用双引号和单引号转义\n不要添加内联事件处理程序,尤其是在您已经在使用JQuery时!低调地使用javascript。避免使用onclick属性绑定事件,而是使用
.on()
把那些该死的花括号修好!天哪,我的眼睛hurt@jjj他仍然需要将字符串作为数据属性存储在元素上,这样就不会为他保存任何代码。