首页 > 解决方案 > 如何在没有 ?"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".

标签: javascripthtmlnode.jspug

解决方案


改变了

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>


推荐阅读