简体   繁体   English

将对象传递给javascript中的html函数?

[英]Pass object to function in html in javascript?

I'm trying to pass an object on a button click, but it doesn't pass the object properly. 我试图在单击按钮时传递对象,但是它无法正确传递对象。 How can I fix this? 我怎样才能解决这个问题?

function addEventToCart(event) {
    $("#mentorItems").append(
        `<div id="${event.id}" class="alert alert-dismissible">
        <button class="close" data-dismiss="alert" onclick=removeEvent(event)>&times;</button>
        </div>`);

The removeEvent function looks something like this: removeEvent函数如下所示:

function removeEvent(event) {
     var color = event.color;
 }

All the properties in event are undefined, so I assume something is going wrong in the passing? 事件中的所有属性都是不确定的,因此我假设传递过程中出现了问题? I've tried to follow this solution but no success either. 我尝试遵循解决方案,但也没有成功。

The event is undefined object because you can't pass an object as plain text (you did it when you wrote removeEvent(event) I would try changing the structure: event是未定义的对象,因为您不能将对象作为纯文本传递(您在编写removeEvent(event)就做到了removeEvent(event)我将尝试更改结构:

function addEventToCart(evt) {
    var $div = $('<div id="' + evt.id + '" class="alert alert-dismissable"></div>');
    var $btn = $('<button class="close" data-dismiss="alert">&times;</button>');
    $btn.on('click', function() {
        var color = evt.color;
        alert(color);
    });
    $div.append($btn);
    $('#mentorItems').append($div);
}

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

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