【问题标题】:Button in bootstrap popover loses attributes引导弹出窗口中的按钮丢失属性
【发布时间】:2020-04-27 00:47:47
【问题描述】:

我的 django 网站有一个引导弹出窗口,当单击按钮时会打开。在此弹出框内是另一个按钮。我目前正在通过将 HTML 放入弹出框的“数据内容”中来执行此操作,如下所示:

<a tabindex="0" id="{{prod.title}}" value="{{prod.id}}" type="button" 
class="btn btn-secondary btn-light mt-3 mb-0" 
data-toggle="popover"data-trigger="focus" data-html="true" data-content="

<div class='btn-group-vertical'>
<a type='button' data-show-value='{{prod.id}}' id='{{prod.id}}'
 class='btn btn-secondary btn-pop wish'>Add to Wishlist</a>
</div>"

>More Options</a>

我需要在 jQuery 中获取“数据显示值”,我目前正在使用以下内容,当单击此按钮时会触发:

 $(document).on('click', '.wish', function () {

        var thisButton = $(this)[0]
        console.log(thisButton);
        var prodID = $(this).data("show-value");
}

但是,所有这些都是返回“未定义”。

我使用console.log(thisButton)查看按钮代码显示的内容,是这样的:

<a id=10 class='btn btn-secondary btn-pop wish'>Add to Wishlist</a>

这解释了为什么“data-show-value”返回未定义,因为属性本​​身没有呈现在网页上。

这是为什么?

【问题讨论】:

    标签: javascript jquery html django bootstrap-4


    【解决方案1】:

    .wish 是一个类,可能有很多元素都有这个类,你需要指定一个 id:

    $(document).on('click', '.wish', function () {
            var thisId = $(this).attr('id');
            console.log(thisId); //check if this is the id of the element you want to trigger
            var prodID = $('#'+thisId).data("show-value");
    }
    

    【讨论】:

    • 谢谢,但是一旦我尝试过,它 prodID 仍然存储未定义
    • console.log(thisId); 返回什么?
    • 返回正确的 ID,但是当我 console.log(var prodID = $('#'+thisId)) 它返回的标签只有 ID 和类属性,没有数据属性.
    • 可以使用id属性提取商品id吗?
    • 最后,我将 HTML 标签从属性中提取到文档本身,并通过 ID 引用它。似乎已经解决了问题
    猜你喜欢
    • 2021-05-02
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2021-03-07
    • 2011-02-25
    相关资源
    最近更新 更多