【问题标题】:Object.assign results in r.Event objectObject.assign 结果为 r.Event 对象
【发布时间】:2016-12-29 12:21:14
【问题描述】:

我正在尝试 console.log 一个已从其他 2 个对象组合的对象。我第一次使用 console.log(allObjects) 时,它起作用了,我看到了这个:

对象 {name: "david", wineResults: "[{"Id":158020,"Name":"Antinori 蒂尼亚内洛 2013","U…"List":[]},"Retail":null,"Vintages":{"List":[]}}]"}

$(document).on('submit', '.add-item-form', function(event) {
    event.preventDefault();

    if (event.type === 'keypress' && event.which === 13 || event.type === 'submit') {

        var name = $('#item-input').val();
        var usersObject = {
            "name": name
        };


        var searchArray = $('#search-results-array').val();
        var searchArrayObj = {
            "wineResults": searchArray
        };

        var allObjects = Object.assign(usersObject, searchArrayObj);
        console.log(allObjects); // This one works great.

        favDropDownItem(allObjects);
        FavoritesList(allObjects);

        $('#item-input').val('');
    }

然后在下面的代码中,我尝试将 allObjects 传递给我创建的链接,该链接显示在下拉菜单中。

function favDropDownItem(allObjects) {
  var output = '';

        output += "<a href='javascript:void(0)' class='dropbtn' onclick='myDropdown()'>Favorites</a>";
        output += "<div class='dropdown-content' id='myDropdown'>";
        output += "<p class='nameLink'>" + name + "</p>";
        output += "<a href='#'>sample</a>";
        output += "</div>";

        $('li.dropdown').html(output);


        $(".nameLink").on('click', function(allObjects) {
            console.log(allObjects);  // This one results in r.Event {originalEvent: MouseEvent, type: "click", target: 
            //p.nameLink, currentTarget: p.nameLink, relatedTarget: null…}
        });
    }

});

当我点击 .nameLink 时,我希望获得与第一个 console.log 中相同的数据,但我看到的是:

r.Event {originalEvent: MouseEvent, type: "click", target: p.nameLink, currentTarget: p.nameLink, relatedTarget: null…}

感谢任何帮助。

【问题讨论】:

    标签: javascript object


    【解决方案1】:

    更改事件参数名称

            $(".nameLink").on('click', function(e) {
                console.log(allObjects);  //now you can get correct out put here
            });
    

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 2017-03-01
      相关资源
      最近更新 更多