将当前页面URL提交为同一页面上具有多个表单的隐藏字段(Javascript)

问题描述 投票:0回答:2

我正在网站上设置某些表格,同一表格将在一页上多次出现(这就是为什么我在这里不能使用getElementById的原因。

我已尽一切努力使当前URL作为隐藏字段传递,但我无法弄清楚。谢谢您的帮助!

HTML

<form>
    <input type="email" name="email-address" placeholder="Your work email" />
    <input type="button" class="btn btn-inline" value="Submit info" />
    <input type="hidden" name="convertURL" value="" />
</form>

当前脚本

<script>
    document.getElementsByName("convertURL").value = window.location.href;
</script>

表单提交脚本

[如果有帮助,这是表单的提交方式:

<script>
$('input[type="button"]').click(function(e){
    e.preventDefault();
    $.ajax({
        url:'https://examplesite.com/',
        type:'post',
        data:$(this.form).serialize(),
        success:function(){
          window.location = "/success";
        }
    });
});</script>
javascript html forms dom
2个回答
0
投票

如果您使用的是jQuery,它将解决它。

$('[name=convertURL]').val(window.location.href);

0
投票
$('input [type =“ button”]')。click(function(e){e.preventDefault();让currentUrl = window.location.href;让数据= $(this.form).serializeArray();//从表单中删除输入,并按如下所示附加网址,data.push({name:'convertURL',value:currentUrl});$ .ajax({网址:“ https://examplesite.com/”,类型:“ post”,数据:数据,成功:function(){window.location =“ /成功”;}});});
© www.soinside.com 2019 - 2024. All rights reserved.