我在将对象从已注册事件发送到 JavaScript 函数时遇到问题。这是复选框 onchange 代码;在 test()
函数中 figure
是 object
:
$('#tblEntity').append('<tr><td><input type="checkbox"
checked="checked"
onchange=test("' + figure + '","' + id + '")
/></td><td>' + Column.figure.text + '</td></tr>');
这是测试
函数:
function test(that, id) {
alert(id+" "+that);
}
当我在 IDE 的运行时 View 中看到已注册的函数时,它的格式不正确:
function onchange(event){
test([object
}
最佳答案
您可以在此处查看我的示例:http://jsfiddle.net/j608x6g7/
您正在将对象转换为字符串。因此对象的字符串版本是:“[Object object]”。您应该传递一个包含您的对象和您的 ID 的回调函数。
// This is what you will put into the onChange
// event. It wraps 'figure' and 'id'. It's
// a function callback !
var callbackTest = function(){
test(figure, id);
}
// Your input checkbox
// Declared in jQuery but this is
// optional
var input = $("<input>")
.attr("type", "checkbox")
.attr("checked", "checked")
.on("change", callbackTest)
关于javascript - 如何从已注册的事件发送 JavaScript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29446721/