【问题标题】:How do I get the value from HTML/JADE input without ?"name" = "value"如何在没有 ?"name" = "value" 的情况下从 HTML/JADE 输入中获取值
【发布时间】:2018-06-21 06:42:13
【问题描述】:

我目前正在创建一个搜索页面,我可以在其中按 ID 获取用户的个人资料。

这是我的 JADE 表单,我在其中输入了我的用户 ID,然后我提交表单以发送帖子:

<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"

【问题讨论】:

  • arrt() 是一个 jQuery 方法,它不起作用
  • 与问题无关,但 Jade 多年前更名为 Pug。

标签: javascript html node.js pug


【解决方案1】:

改变

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>

【讨论】:

  • @ppfernandes 哎呀,我的错。改成 JavaScript 一个
  • @ppfernandes 我认为我的代码会将 id 附加到表单的现有操作
  • @ppfernandes 还有alter(docid)到底在做什么?
  • @ppfernandes 这个form.action= form.action+docid.value; 不适合你吗?我看不出它不应该工作的任何原因
  • @ppfernandes 能否请您粘贴 JADE 生成的简单 html 表单,以便我检查哪里出错了。我认为您在全局范围内做错了,我正在修改代码,请尝试该代码
猜你喜欢
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多