javascript - 当我在 url 处输入 ?variable=value 时如何更改值?
问题描述
情况:
- 输入值为空,类型为隐藏。
- 我在 url 输入 ?hiddenname=007
- 我单击按钮“提交”并导致函数调用()..
预计:
- 系统根据 ?hiddenname=007 在 url 使用函数 call() 将值填入输入
- 之后,系统将值为 007 的表单发送到服务器。
当前结果:
- 系统仍然发送空值的表单。
网址
localhost/index.html?hiddenname=007
索引.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>
结束.php
<?php
echo $_GET['hiddenname'];
?>
解决方案
这将在窗口加载时hiddenname
从 URL 查询字符串中获取值,然后设置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;
};
推荐阅读
- lstm - 如何在 LSTM 中制作堆叠的编码器-解码器
- javascript - 意外的保留词“等待”
- angular - Angular 6 - Sonarqube 覆盖率报告始终为 0,但业力显示覆盖率
- google-cloud-platform - 如何知道我在 GCP 中拥有的所有权限?
- node.js - 如何更改 api 响应
- cors - Google Cloud IAP CORS 政策阻止我的网络应用调用受 IAP 保护的 API
- javascript - Bitrix24 BX24.callBatch js 语法
- c# - Context.update() 无法跟踪实体类型“模型”的实例,因为已在跟踪另一个具有键值“{Id: 12}”的实例
- regex - 从文件中搜索和替换多行
- java - Flutter中移动应用主屏幕的小部件