我想通过 onsubmit 将 html 参数添加到 url 中。我有 2 个表单(1 个 GET,1 个 POST),想要使用 1 个按钮来提交它们(使用 POST 表单按钮)当按下提交按钮时,使用 onSubmit 调用 JavaScript 函数,其中参数“附加”到网址。
我正在考虑做这样的事情:
function onSubmitForm() {
url = "localhost:8080/test/index.php?Title=document.getElementsByName('title')[0].value";
//I don't know how to actually call the url.
}
编辑
最佳答案
首先,您必须连接字符串的静态和动态部分。然后,您可以重定向。
function onSubmitForm() {
window.location = "localhost:8080/test/index.php?Title=" +
document.querySelector('title').textContent;
}
注释:
- 只有表单字段具有
.value
属性(property)。如果你想获得 元素内的文本,您可以使用.textContent
. -
.getElementsByName()
扫描整个文档并制作 所有匹配元素的集合。当你 知道您只想要第一个,在<title>
的情况下, 那里 无论如何,它只会成为文档中的其中之一。使用.querySelector()
仅定位第一个匹配元素。
关于javascript - 使用 Javascript 函数通过 onSubmit 添加参数到 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49058337/