【问题标题】:Anchor tag with mailto and javascript function带有 mailto 和 javascript 功能的锚标记
【发布时间】:2014-04-22 11:52:35
【问题描述】:

下面是我的一段代码,当用户单击锚链接时,我试图调用一个 javascript 函数和 mailto,我的 javascript 函数抛出错误,如果我删除 mailto 它工作正常。如果我在这里遗漏任何东西,请告知。

    <a rel="nofollow" onclick="javascript:confirm('2R','ADDRESS');return true;" href="mailto:housing@test.edu?
subject=Incorrect Residential Address">housing@test.edu (Click Here)</a>

这是我的确认函数,它有 ajax 调用

    function confirm(addcode,type){

     $.ajax({
            url: 'https://mysite.url/confirm',
            dataType: 'json',
            method: 'POST',
            cache : false,
             beforeSend: function setHeader(xhr){
                 console.log(addcode + '<--->'+type+'<--->'+term);
                $("#main-content").mask("Please wait...");
            },
            data: {
                'addCode': addcode,
                'type': type,
                'termCode' : term
            },
            success: function(data){
                notify("Confirmed");
                $("#main-content").unmask(); 
            },
            error: function(xhr, testStatus, error) {
                $("#main-content").unmask(); 
                notify("Unable to Confirm");

                console.log(error);
                console.log(xhr);
                console.log(testStatus);
            }
        });
}

【问题讨论】:

  • 什么错误?可以分享一下吗
  • 似乎工作 -> jsfiddle.net/S5jHS
  • 当然,您需要告诉我们错误并向我们展示 javascript 代码
  • 如果是函数引发了错误,那么我们需要看看。
  • 确认函数是我在java-script中定义的,它有一个ajax调用,

标签: javascript jquery html dom


【解决方案1】:

还不能评论: 我认为这不是一个真正的“错误”,更多的是代码没有按照他的预期工作。

  • 在激活“mailto:”之前需要一个确认框。
  • 如果用户按下是,则会激活它。
  • 如果用户按下否,您希望它什么也不做。

这是一个跨浏览器解决方案: jsfiddle:http://jsfiddle.net/kychan/9KuLM/

function mConfirm(e, a)
{
    if (confirm('Are you sure you want to send it to: ' + a + '?'))
    {
        return true;
    }
    else
        e.preventDefault();
}

编辑:

顺便说一句。您不能更改确认框的标题。 'confirm()' 函数仅适用于 1 个参数。如果您坚持制作带有标题的确认框,则应使用第三方脚本或您自己的函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-11
    • 2011-11-12
    • 2011-08-04
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多