如何解决在 url 中键入 ?variable=value 时如何更改值?
情况:
- 输入值为空,类型隐藏。
- 我在网址中输入 ?hiddenname=007
- 我点击了“提交”按钮并导致函数调用()..
期待:
- 系统根据 ?hiddenname=007 at url 使用函数 call() 将值填入 input 中
- 之后,系统将值为 007 的表单发送到服务器。
当前结果:
- 系统仍然发送空值的表单。
网址
localhost/index.html?hiddenname=007
index.html
<!DOCTYPE html>
<html>
<head>
<script>
function call(){
document.forms["form1"].hiddenname.value=hiddenname;
console.log("function run");
}
</script>
</head>
<body>
<form name="form1" action="end.php">
<input type="hidden" id="hiddenid" name="hiddenname" value="" />
<input type="submit" id="send" onclick="call()"/>
</form>
</body>
end.php
<?php
echo $_GET['hiddenname'];
?>
解决方法
这将在窗口加载时从 URL 查询字符串中获取 hiddenname
的值,然后设置 hiddenid
输入的值:
window.onload = function () {
// get the search params from the window's URL
const urlParams = new URLSearchParams(window.location.search);
// get the value of 'hiddenname'
const myParam = urlParams.get('hiddenname');
// store this value in the input with id="hiddenid"
document.getElementById('hiddenid').value = myParam;
};
,
将 return true;
添加到 call() 函数。
将 onsubmit="return call();"
添加到表单属性。
并从提交按钮中删除 onclick 功能。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。