如何使用jquery获取twig变量的属性值

问题描述 投票:0回答:1

我有以下问题

<a href='#' data-entry-id='{{ income.incSum }}' id='some_link_1'></a>
const $entryElements = $('[data-entry-id]');

     const $entryIds =
         $.map($entryElements, item => $(item).data('entryId'));
     console.log("entry ids ", $entryIds);

如果 data-entry-id 属性位于方括号中,则一切正常,将显示值。

只有我想获取id为some_link_1的data-entry-id属性的值(因为我在页面上有几个data-entry-id值):

<a href="#" data-entry-id="{{ income.incSum }}" id="some_link_1"></a>

我为此使用以下符号:

const $entryElements2 = $('#some_link_1').attr('[data-entry-id]');
const $entryIdsEle = $.map($entryElements2, item => $(item).data('entryId'));
console.log("mine is: ", $entryIdsEle);

如果 data-entry-id 在方括号中,则值为空,但如果不带方括号:

const $entryElements2 = $('#some_link_1').attr('data-entry-id');

发生错误

enter image description here

请帮助我如何使用属性和 ID 名称获取此数据。

我使用了这篇文章https://cruftlesscraft.com/passing-data-from-twig-to-javascript

html jquery twig
1个回答
0
投票

尝试这样的事情

    let els = $('a[data-entry-id]');
    console.log(els);
    let el1;
    els.map((key, el) => { console.log(el); });
    els.filter((index, elt) => console.log(elt));
© www.soinside.com 2019 - 2024. All rights reserved.