单击按钮执行2个操作:文件下载&注册

时间:2017-10-24 18:23:49

标签: javascript html css forms

我正在努力使其无论何时单击订阅按钮,它都会执行注册(如在演示的代码中),但也会下载文件。

我尝试添加onclick =" window.location.href ='#'“,但没有任何反应。我考虑过使用javascript,但我不认为订阅可以这样工作,但我相信文件可以这样打开。

是否有一种简单的方法可以满足这两种结果?其他帖子不包括电子邮件注册,这会导致问题。非常感谢一些帮助;)

<form action="http://someuniquemailchimpurl.us16.list-manage.com/subscribe/post?u=d6f1&amp;id=9ebc2randomlink" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>

    <input type="email" placeholder="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">

<div id="mce-responses">

        <div class="response" id="mce-error-response" style="display:none"></div>

        <div class="response" id="mce-success-response" style="display:none"></div>

    </div>

    <input type="submit" value="Subscribe" name="subscribe" id="newsletter-button">

</form>

2 个答案:

答案 0 :(得分:1)

首先,让我首先说,使用一个按钮执行两个完全不同的操作可能不是一个好主意。但是,您更了解您的业务,以便您做出决定,但请考虑此建议。如果您仍然想要使用它,您有几个选项,但所有选项都涉及JavaScript。

在我们选择这些选项之前,建议您从target="_blank"标记中删除form。我在下面的示例中删除了它。

一种选择是像您尝试的那样使用onclick事件。你这样做的方式不会起作用,因为它会重新加载页面而订阅事件不会发生。您需要在弹出窗口中下载该文件。这是一个例子:

<form action="http://someuniquemailchimpurl.us16.list-manage.com/subscribe/post?u=d6f1&amp;id=9ebc2randomlink" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" novalidate>
<input type="submit" onclick="downloadFile()" value="Subscribe" name="subscribe" id="newsletter-button">
</form>
<script type="text/javascript">
    function downloadFile() {
        var dw = window.open();
        dw.location.href = "the_file_url";
    }
</script>

另一个选项是使用onsubmit标记上的form事件提交表单时下载文件。这是一个例子:

<form action="http://someuniquemailchimpurl.us16.list-manage.com/subscribe/post?u=d6f1&amp;id=9ebc2randomlink" method="post" onsubmit="downloadFile()" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" novalidate>
<input type="submit" value="Subscribe" name="subscribe" id="newsletter-button">
</form>
<script type="text/javascript">
    function downloadFile() {
        var dw = window.open();
        dw.location.href = "the_file_url";
    }
</script>

答案 1 :(得分:1)

您可以使用事件绑定并执行两个操作。将帖子操作更改为下载操作(或相反),然后:  使用jQuery:

$('#newsletter-button').click(function (e) {
    e.preventDefault();

    //although it will not complete properly, ajax call should be ok to signup url
    $.ajax({
        url: 'URL-FROM-SIGN-UP',
        complete: function () {
            // here we submit the form, get(0) needs because submit() from jQuery does not submit a form
            $('#mc-embedded-subscribe-form').get(0).submit();
        }
    });
});