简体   繁体   English

如何在js函数中将对象作为函数参数传递?

[英]How to pass a object in js funcion as a function parameter?

I have a jquery foreach appended list in a table. 我在表中有一个jQuery foreach附加列表。 what I want is to pass the full object that I am getting from an ajax success function.but when I try to pass the Object as a parameter to a different JS function, the parameter gets undefined. 我想要的是将我从ajax成功函数获取的完整对象传递给我,但是当我尝试将Object作为参数传递给另一个JS函数时,该参数变得不确定。

this is my appended table: 这是我的附表:

 $.ajax({ type: "post", url: "@Url.Content("~/Estimate/GetOffsetLetterHeadCost ")", data: $('#OffLetterHeadForm').serialize(), datatype: "json", traditional: true, success: function(data) { var Json = data; $.each(Json, function(index, obj) { var row = '<tr>' + '<td style="padding:10px" > <b style="font-size:18px">' + obj.VendorName + '</b> </td> ' + '<td><label id="machineId' + index + '">' + obj.MachineName + ' < / label > < /td > ' + '<td> <input type="button" value="Order" id="btn' + index + '"onclick = "SaveOffsetOrder(obj)" / > < /td></tr > '; $("#AllVendorsList").append(row); }); } }); function SaveOffsetOrder(obj) { // here i am getting obj undefined // do something with obj } 
 <table id="AllVendorsList" class="table table-striped table-hover " style="font-size:14px; font-family:'Trebuchet MS'"></table> 

please help..Thanks 请帮助..谢谢

You need to specify onclick as onclick='SaveOffsetOrder( + JSON.stringify(obj) + )' notice that single quote before SaveOffsetOrder and JSON.stringify(obj) which will create onclick function as SaveOffsetOrder({"name":"test"}) which is correct. 您需要将onclick指定为onclick='SaveOffsetOrder( + JSON.stringify(obj)+ )'注意,在SaveOffsetOrderJSON.stringify(obj)之前的单引号将创建onclick函数为SaveOffsetOrder({"name":"test"})是正确的。

 var obj = {name: 'test'}; var html = `<input type="button" value="Order" id="btn' + index + '" onclick='SaveOffsetOrder(` + JSON.stringify(obj) + `)' />`; document.getElementById('content').innerHTML = html; function SaveOffsetOrder(obj){ console.log(obj); } 
 <div id='content'></div> 

Instead of using onclick use bind which will make life pretty much easier. 与使用onclick相比,使用bind可以使生活变得更加轻松。 Eg 例如

'<td> <input type="button"   value="Order" id="btn' + index + '" 
      / > < /td></tr > '
$("#AllVendorsList").append(row);
$("#btn" + index).bind("click",{obj:obj},SaveOffsetOrder);

function SaveOffsetOrder(e){
 console.log(e.data.obj);//this will return your object.
}

it's easy to mess up variable and string/text within quotation, so colors will help a lot. 将引号内的变量和字符串/文本弄乱很容易,因此颜色会很有帮助。 make sure all variable 'pop out' more. 确保所有变量都“弹出”。

if still doesn't work, switch all single quotes to double, and replace all double quotes in position of single quote. 如果仍然不起作用,请将所有单引号切换为双引号,并在单引号的位置替换所有双引号。 or, discard and just use selector later in script rather than using onclick property 或者,放弃并仅在脚本中稍后使用选择器,而不是使用onclick属性

EDITED (added escape characters in function parameter \\' ) 编辑(在功能参数\\'添加了转义字符)

<html>
<body>
    The content of the body element is displayed in your browser.
<script>
    var obj = "hello world";
    var row = '<tr>\
        <td style="padding:10px" >\
            <b style="font-size:18px">'+obj+'</b>\
        </td>\
        <td>\
            <label id="machineId'+obj+'">'+obj+'</label>\
        </td>\
        <td>\
            <input type="button" value="Order" id="btn'+obj+'" onclick = "SaveOffsetOrder(\'' + obj +' \')"/>\
       </td>\
    </tr>';

    document.body.innerHTML = row;
    function SaveOffsetOrder(obj){
        alert(obj);
    }
</script>
</body>
</html>

----before (only works for integers?)----- ----之前(仅适用于整数吗?)-----

var row = '<tr>\
    <td style="padding:10px" >\
        <b style="font-size:18px">'+obj.VendorName+'</b>\
    </td>\ 
    <td>\
        <label id="machineId'+index+'">'+obj.MachineName+'</label>\
    </td>\ 
    <td>\
        <input type="button" value="Order" id="btn'+index+'" onclick = "SaveOffsetOrder(' + obj + ')"/>\
    </td>\
</tr>';

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

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