我需要一种方法来向数据库或文本文件提交电子邮件,并且只需单击一下即可转到链接



我正在一个职业介绍所网站上工作,他们希望我添加一个功能,有人会输入他们的电子邮件地址,当他们点击提交时,它会将其放入文本文件中进行存储,然后将他们重定向到申请页面。

这就是我得到的,但它不起作用

$('#link').click(function(e){
  var cur = $(this).attr('href')
  $(this).attr('href',cur + '&email=' + escape($('#email').val()))
})

您需要重定向到链接:

window.open('your link')

例:

$('#link').click(function(e){
  var href = $(this).attr('href')
  var newHref = href + '&email=' + escape($('#email').val())
  // you only need ONE of these next two lines....
  window.open(newHref); // this would open in a new window/tab
  document.location = newHref; // this would redirect the current page to the new link
  e.preventDefault(); // this prevents the link from working with the wrong href
})

这样的事情会起作用。 您需要对服务器端页面进行 ajax 调用并保存电子邮件地址,然后将用户重定向到所需的页面。

$('#link').click(function(e) {
    e.preventDefault();
    var email = $('#email').val(),
        href = $(this).attr('href'),
        newHref = href + '&email=' + escape(email);
    $.ajax({
        type: "POST",
        url: '/SaveEmail',
        data: {'email': email},
        success: function() {
            // email saved successfully
            window.location = newHref;
        },
        error: function() {
            // server error while saving email
            alert('Error occurred!')
        }
    })
});

相关内容

最新更新