javascript - 将当前页面 URL 作为隐藏字段提交,同一页面上有多个表单(Javascript)
问题描述
我在一个网站上设置了一些表单,其中同一个表单会在一个页面上出现多次(这就是我不能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);
推荐阅读
- c# - FileStream 更改 excel 扩展名
- azure-devops - 如何在 Azure Devops 任务组中将一个参数的值传递给另一个参数
- c# - 为什么 MahApps 找不到 Controls.AnimatedTabControl.xaml?
- lua - 使用 Tween 服务时“尝试调用 TweenInfo 值”
- python - pdfminer3 没有从彩色 pdf 页面中提取文本,如何将 pdf 页面转换为灰度?
- c++ - 额外的行作为 C++ 中的输出出现
- c++ - 如何在 wxRichToolTip、wxWidgets 中添加按钮?
- python - 删除重复项并使用最旧的值完成 nan 并优化运行时间
- javascript - 拖入反应后如何显示下一张幻灯片?
- amazon-web-services - 如何将变量导入 AWS ApiGateway 的 Swagger 文件