javascript - 如何在没有 ?"name" = "value" 的情况下从 HTML/JADE 输入中获取值
问题描述
我目前正在创建一个搜索页面,我可以在其中通过 ID 获取用户的个人资料。
这是我输入用户 ID 的 JADE 表单,然后我提交表单以发送帖子:
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>List Funcionários</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" media="screen" href="/admin/css/main.css"/>
</head>
<body>
<header class="header">
<h1 id="title" class="inline"><img id="logo" src="/admin/image/logo4.png" alt="Image failed to load" class="inline"/>User Med</h1>
<h2 id="stitle">Search User</h2>
</header>
<div class="do">
<form id="search-theme-form" action="/profileuser/" accept-charset="UTF-8" method="get" name="search-theme-form" class="myDform">
<label>User ID:
<input id="inputDoc" name="id" required="" value="" type="text" class="input"/>
</label>
<input value="Submit" type="submit"/>
</form>
</div>
<script src="./admin/scripts/profileuser.js"></script>
</body>
</html>
这是我的 JS 代码,我尝试在其中获取输入值并将表单操作更改/profileuser/
为/profileuser/"userid"
:
let docid = document.getElementById('inputDoc').value;
let form = document.getElementsByClassName('myDform');
form.addEventListener('submit', submit);
function chgAction(){
alter(docid)
form.chgAction = docid.value;
}
function submit (e){
chgAction();
}
问题是我不能只从输入中获取用户 ID,而是得到?id= "userid"
.
解决方案
改变了
form.chgAction = docid.value;
和
form.action = form.action+docid.value;
document.getElementById('search-theme-form').addEventListener('submit',chgAction);
function chgAction(){
let docid = document.getElementById('inputDoc');
let form = document.getElementById('search-theme-form');
form.action= form.action+docid.value;
console.log("path after: "+form.action);
}
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>List Funcionários</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" media="screen" href="/admin/css/main.css"/>
</head>
<body>
<header class="header">
<h1 id="title" class="inline"><img id="logo" src="/admin/image/logo4.png" alt="Image failed to load" class="inline"/>User Med</h1>
<h2 id="stitle">Search User</h2>
</header>
<div class="do">
<form id="search-theme-form" action="/profileuser/" accept-charset="UTF-8" method="get" name="search-theme-form" class="myDform">
<label>User ID:
<input id="inputDoc" name="id" type="text" class="input" required/>
</label>
<input value="Submit" type="submit"/>
</form>
</div>
</body>
</html>
推荐阅读
- angular - 如何以编程方式更改 ionic v4 中的切换
- javascript - 修复 JS 代码:将复选框中的多个值附加到 url
- c++ - 抛出异常:读取访问冲突。**_Right_data** 为 0x8。发生了
- css - 如何使父高度包裹内容高度?
- c++ - C++ 混乱中的插入器
- python - 有没有办法将列表值作为自定义聚合列注入查询集中
- ruby-on-rails - Activerecord 查询搜索零枚举
- reactjs - 更改父通道的道具时如何重新渲染React子组件(redux)
- docker - 将 docker run 中的网络选项转换为 docker-compose?
- c# - DataGridView 不显示某些数据