【问题标题】:Append the values to the URL on Form in Javascript在 Javascript 中将值附加到表单上的 URL
【发布时间】:2017-01-02 15:31:56
【问题描述】:

我在多个页面上有一个表单链接。当我单击页面 A 的表单链接时,它应该在 URL 上显示为 www.form.com?referrer=Page A 并且当我提交表单时,我创建了一个隐藏字段 referrer,当我在我的电子邮件中的推荐人字段上收到它时它应该显示“页面 A”。

我有这个工作正常的 HTML 代码,但是我不想在每个页面上手动执行,一旦用户单击表单链接或收到它应该自动更新的表单:

<a target="_blank" title="Click Here" href="https://myform.com/forms/land_discount?referrer= Beach Club">

这是我的表单的 JavaScript:

Discount Form

【问题讨论】:

  • 你能澄清一下吗?你是说如果你点击这个链接https://myform.com/forms/test,它会自动附加到https://myform.com/forms/test?referrer=Whatever+Page+this+is或者是https://myform.com/forms/test?referrer=Game+Form或者https://myform.com/forms/test?referrer=Online+Form或者别的什么?
  • 嗨 Rasckatt..我已经编辑了我的问题..如果我点击了页面 A..on url 上的表单,它应该是“www.form.com?referrer=Page A”..我想第一个选项
  • 我在您的代码中没有看到任何表单。
  • 所以它会读取页面的 URI 并将页面附加到 ?referrer=page/name?
  • @Rasclatt 是的..这是我想要的预期结果

标签: javascript php forms get


【解决方案1】:

看看这是不是你想要做的:

// I'm going to use jQuery, it's easier for me
$(document).ready(function(){
    // I used a class so it doesn't do it to every <a> tag
    $('a.use_refer').click(function(e){
        // Stop the button from submitting normally
        e.preventDefault();
        // Create a new url
        // You may or may not want to use encodeURIComponent() on the path value
        var newUrl  =   $(this).attr('href')+'?referrer='+document.location.pathname;
        // Go to the new page
        window.location = newUrl;
    });
});
</script>

<a href="https://myform.com/forms/land_discount" title="Online Form" class="use_refer">Click Here</a>

【讨论】:

    【解决方案2】:

    假设你Page A 是路径的最后一个目录。并在 DOM 准备好时包装它。

    注意添加了id= 标记对HTML 的更改。

    在“页面 A”上

    HTML:

     <script type="text/javascript" src="https://myform.com/forms/js.php/test"></script>
     <noscript>
       <a href="https://myform.com/forms/test" title="Online Form">Game Form</a>
     </noscript>
    

    Javascript:

    var url = document.getElementById('formRef').getAttribute("href");
    document.getElementById('formRef')
    .setAttribute('href', encodeURI(url + '?referrer=' + window.location.href.slice(window.location.href.lastIndexOf('/') + 1)));
    

    在“登陆页面”上

    HTML:

    <a id="landingPage" target="_blank" title="Click Here" href="https://myform.com/forms/land_discount">
    

    Javascript:

    var href = document.getElementById('landingPage').getAttribute('href');
    
    if(window.location.href.indexOf('referrer=') > -1){
        document.getElementById('landingPage').setAttribute('href', encodeURI(href + window.location.href.slice(window.location.href.indexOf('?referrer='))));
    }
    

    【讨论】:

    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 2012-07-04
    • 2022-01-17
    • 1970-01-01
    • 2013-11-08
    • 2023-03-28
    • 2022-01-05
    • 2011-12-14
    相关资源
    最近更新 更多