我正在网站上设置某些表格,同一表格将在一页上多次出现(这就是为什么我在这里不能使用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>
如果您使用的是jQuery,它将解决它。
$('[name=convertURL]').val(window.location.href);
$('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 =“ /成功”;}});});