jquery在交互后没有监听数据元素

时间:2012-12-21 01:36:44

标签: jquery twitter-bootstrap

我不确定这个的最佳头衔......

Live Link Provided Here

我对这个项目的总体目标。某些文档需要电子邮件才能下载。通过asp.net我绑定一个转发器,然后用jquery删除href并将其放在rel标签中,直到他们添加他们的电子邮件地址。

$('a[data-email="True"]').each(function () {
    var href = $(this).attr('href');
    $(this).attr('href', '#_');
    $(this).attr('rel', href);
});

jquery检查data-email tag =“True”是否会弹出bootstrap modal,然后通过ajax表单调用web method和如果电子邮件不是重复的,则将电子邮件添加到数据库中。

$('.emailcheck').live("click", function (e) {        
    if ($(this).data("email") == "True") {
        $('#emailModal').modal();
    }
});

一旦成功,模式将关闭,并且在每个data-email="True"上,链接将添加回hrefdata-email设置为False

$('#emailModal').modal('hide');
$('body a[data-email="True"]').each(function () {
    var reltag = $(this).attr('rel');
    $(this).attr('href', reltag);
    $(this).attr('data-email', 'False');
});

所有这些都成功发生,并在另一次点击时下载电子邮件。但是,当我点击下载链接时,模态仍会弹出。

有人可以指导我解决为什么模态仍然会出现的问题。在成功脚本中还有一种方法可以自动触发下载吗?

1 个答案:

答案 0 :(得分:1)

试试这个 - 没有太大的改变,但这里有jsFiddle显示它应该有效

OnLoad功能

$('.emailcheck').each(function() {
    var href = $(this).attr('href');
    $(this).attr('href', '#_').attr('rel', href);
});

点击活动

$(document).on("click", '.emailcheck', function(e) {
    e.preventDefault()
    if ($(this).attr("data-email") == "True") {
        $('#emailModal').modal();
    }
});

电子邮件提交功能

$('#emailModal').modal('hide');
$('.emailcheck').each(function() {
    var reltag = $(this).attr('rel');
    $(this).attr('href', reltag).attr('data-email', 'False');
});