javascript 将 JSON 对象传递给 onclick 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20736540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:10:19  来源:igfitidea点击:

Pass JSON object to onclick function

javascriptjqueryjson

提问by user11

I am trying to pass JSON object to onclick function but it does not work.

我正在尝试将 JSON 对象传递给 onclick 函数,但它不起作用。

$.each(matches, function() {                        
    var item_data = { 
        "category" : this['category_name'],
        "stock" : this['stock_name'],
        "supplier_stock" : this['supplier_stock_name']
    };

    available_stock_items = available_stock_items + '<tr> \
        <td>' + this['category_name'] + '</td> \
        <td>' + this['stock_name'] + '</td> \
        <td>' + this['supplier_stock_name'] + '</td> \
        <td align="center"><img src="' + $j('#edit-vardru-base-path').val() + 'sites/all/modules/core/images/add.png" onClick="select_item('+ item_data +');" /></td> \
        </tr>';
});

function select_item(data)
{
    console.log("***********PRINT***************");
    console.log(data);
}

I get this error "Uncaught SyntaxError: Unexpected identifier" in console window.

我在控制台窗口中收到此错误“Uncaught SyntaxError: Unexpected identifier”。

EDIT: Now I am trying to pass data (item_data) using event delegation. I have added following code inside $.each(). Callback function is called at click event but I am not able to access item_data.

编辑:现在我正在尝试item_data使用事件委托传递数据 ( )。我在 $.each() 中添加了以下代码。单击事件时调用回调函数,但我无法访问 item_data。

$j(document).delegate("#row-"+count, "click", function(item_data){
                        alert("TEST");
                        console.log("TEST");                            
                        console.log(item_data);     
                    });

回答by user3123649

You could use html5 data attributes

您可以使用 html5 数据属性

<div id="dataDiv" data-num="">click div</div>

<script>
var jsonData = { "name": "name1" };

$(document).ready(function () {        
    $("#dataDiv").data("num",jsonData);
    $("#dataDiv").on("click", function () {
        console.log($(this).data("num"));
    });
});
</script>

回答by HDT

You can not paste json in DOM try my demo html

你不能在 DOM 中粘贴 json 试试我的演示 html

<div id='show_test'>
</div>

jquery

查询

$(document).ready(function(){
var matches = [
    {"category_name":"category_1","stock_name":"stock_1","supplier_stock":"supplier_stock_1"},{"category_name":"category_2","stock_name":"stock_2","supplier_stock":"supplier_stock_2"},
{"category_name":"category_3","stock_name":"stock_3","supplier_stock":"supplier_stock_3"}, {"category_name":"category_4","stock_name":"stock_4","supplier_stock":"supplier_stock_4"}
    ];
    $.each(matches, function(i) {
    var arrayDt = [];
    arrayDt[i] = { 
        "category" : this.category_name,
        "stock" : this.stock_name,
        "supplier_stock" : this.supplier_stock
    };
    $("#show_test").append("<div><a id='bt_"+i+"'>click</a></div>");
    $("#bt_"+i).click(function(){
       show_data(arrayDt[i]);
    });
});
});
    function show_data(data){
        alert(JSON.stringify(data));
    }

or see at

或见

http://jsfiddle.net/F6FxB/2/

http://jsfiddle.net/F6FxB/2/