将javascript变量传输到插件函数(异步URL)

时间:2016-03-20 09:57:21

标签: javascript jquery

对于我的项目,我需要知道如何将一些变量传输到异步URL。我使用的插件是一个简单的弹出窗口,它调用URL来获取html数据并显示结果。

我需要使用 $(this)因为我有很多具有相同类的URls。我必须传输数据类型(例如:图片)和产品的ID(数据ID)。

我的链接= <a href="#" class="product" data-type="picture" id="1">Check this out</a>

我想做什么(但它不起作用):

$('.product').webuiPopover({
    var productType = $(this).data('type');
    var id = $(this).data('id');
    type:'async',
    url:'/api/popover/'+ productType +'/'+id
});

有可能吗?我怎么能这样做?

注意:这是我使用的插件(github:sandywalker / webui-popover)

2 个答案:

答案 0 :(得分:0)

如果你想添加变量,你可以这样做:

$('.product').each(function(i,t){
    var t = $(t);

    t.webuiPopover({
        type:'async',
        url:'/api/popover/'+ t.data('type') +'/'+ t.data('id')
    });
});

答案 1 :(得分:0)

您必须分别为每个Vector v a元素初始化webuiPopover。做这样的事情:

.product
$('div').each(function(index, el){
  $(el).webuiPopover({
  	title: $(el).data('foo') // Direct access to the data attributes of this element
  })
})