【问题标题】:Modify Html form submit output修改Html表单提交输出
【发布时间】:2020-04-04 16:13:45
【问题描述】:

这当然是一个初学者的问题,但我一直在寻找答案。我想使用以下 html 来控制我的一个项目。当我点击提交按钮时,值将添加到 url。这正是我想要的,但我想修改附加的字符串,使解析更容易。下面的 html 会生成这个 url:

[...]/Interface.html?tail=3&speed=45&col1=37&col2=210

但我希望输出看起来更像这样:

[...]/Interface.html?tail=3&speed=45&col1=37&col2=210&end

简而言之:如何将附件格式化为使用 GET 方法提交后添加的 url?

<title>Alu foil installation</title>
<meta name="generator" content="Bluefish 2.2.10">
<meta name="author" content="">
<meta name="date" content="">
<meta name="copyright" content="">
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
<meta http-equiv="content-style-type" content="text/css">
<meta http-equiv="expires" content="0">



<h1>Alu foil installation</h1>
<form>
  <p>
    <label for="ftail">Tail length:</label><br>
    <select name="tail" id="ftail">

      <option value="0">No tail</option>
      <option value="1">Short tail</option>
      <option value="2">Medium tail</option>
      <option value="3">Long tail</option>
    </select>
  </p>
  <p>
    <label for="speed">Set the speed:</label><br>
    <input type="range" id="speed" name="speed" min="0" max="50">
  </p>
  <fieldset>
    <legend>Color selection:</legend>
    <p>
      <label for="color1">Color 1 hue:</label>

      <input type="range" id="color1" name="col1" min="0" max="255">
    </p>
    <label for="color2">Color 2 hue:</label>

    <input type="range" id="color2" name="col2" min="0" max="255">
  </fieldset>

  <input type="submit" value="OK">

</form>

【问题讨论】:

  • 这是一个 X/Y 问题。你为什么要滥用 URL 结构只是为了让它“更容易”在服务器上解析?服务器知道 GET 参数或请求数组中的每个值
  • 我正在使用 arduino IDE 在 ESP32 上运行它。我想使用这些值将它们提供给我的 arduino 草图。
  • 我很好奇。我的答案在您的环境中有效吗?

标签: html forms url get


【解决方案1】:

引入一个隐藏变量end

<input type="hidden" name="end" value="" />

(额外的= 将附加到end

【讨论】:

  • 要么不发送要么给&amp;end=
  • 或者只是这个&lt;input type="submit" name="end" value="OK"&gt;
  • 甚至&lt;input type="hidden" name="end" value="end" /&gt; :-)
  • 这一切都对我有用。非常感谢你。我没有想出使用隐藏输入类型的想法……太好了!
【解决方案2】:

取决于您的 Arduino 或 ESP32 可以做什么,这是一个干净的解决方案

提交:

window.addEventListener("load",function() {
  document.querySelector("form").addEventListener("submit",function(e) {
    e.preventDefault();
    const sp = new URLSearchParams(new FormData(this)).toString();
    console.log("/Interface.html?"+sp+"&end"); // send this
  })
})
<title>Alu foil installation</title>
<meta name="generator" content="Bluefish 2.2.10">
<meta name="author" content="">
<meta name="date" content="">
<meta name="copyright" content="">
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
<meta http-equiv="content-style-type" content="text/css">
<meta http-equiv="expires" content="0">



<h1>Alu foil installation</h1>
<form>
  <p>
    <label for="ftail">Tail length:</label><br>
    <select name="tail" id="ftail">

      <option value="0">No tail</option>
      <option value="1">Short tail</option>
      <option value="2">Medium tail</option>
      <option value="3">Long tail</option>
    </select>
  </p>
  <p>
    <label for="speed">Set the speed:</label><br>
    <input type="range" id="speed" name="speed" min="0" max="50">
  </p>
  <fieldset>
    <legend>Color selection:</legend>
    <p>
      <label for="color1">Color 1 hue:</label>

      <input type="range" id="color1" name="col1" min="0" max="255">
    </p>
    <label for="color2">Color 2 hue:</label>

    <input type="range" id="color2" name="col2" min="0" max="255">
  </fieldset>

  <input type="submit" value="OK">

</form>

改变

//let url = new URL(location.href) // if you need to save it
let tId;
window.addEventListener("load",function() {
  document.querySelector("form").addEventListener("change",function(e) {
    clearTimeout(tId); // they are fiddling
    const sp = new URLSearchParams(new FormData(this)).toString()+"&end";
    //url.search=sp
    tId = setTimeout(function() { location.search = sp }, 2000); // allow the user to modify the controles
  })
})
<title>Alu foil installation</title>
<meta name="generator" content="Bluefish 2.2.10">
<meta name="author" content="">
<meta name="date" content="">
<meta name="copyright" content="">
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
<meta http-equiv="content-style-type" content="text/css">
<meta http-equiv="expires" content="0">



<h1>Alu foil installation</h1>
<form>
  <p>
    <label for="ftail">Tail length:</label><br>
    <select name="tail" id="ftail">

      <option value="0">No tail</option>
      <option value="1">Short tail</option>
      <option value="2">Medium tail</option>
      <option value="3">Long tail</option>
    </select>
  </p>
  <p>
    <label for="speed">Set the speed:</label><br>
    <input type="range" id="speed" name="speed" min="0" max="50">
  </p>
  <fieldset>
    <legend>Color selection:</legend>
    <p>
      <label for="color1">Color 1 hue:</label>

      <input type="range" id="color1" name="col1" min="0" max="255">
    </p>
    <label for="color2">Color 2 hue:</label>

    <input type="range" id="color2" name="col2" min="0" max="255">
  </fieldset>


</form>

【讨论】:

  • 我对 html 和其他东西真的很陌生,对不起。你能告诉我把第一个块放在哪里(以“window.addEventListener”开头)吗?它是转到外部文件还是嵌入在第二个块中定义的 html 文件中?除此之外:是否有可能在每次用户进行输入(例如用户移动滑块)时更改“addEventListener("submit",function(e)”来修改 url 而无需按下“提交” ” 按钮?这将是惊人的......
  • 第一个块可以原样进入外部 js 文件。如果你调用那个 jscode.js,那么你需要在 HTML 的头部添加&lt;script src="jscode.js"&gt;&lt;/script&gt;。如果要嵌入它,只需将第一个块包装在 &lt;script&gt; /* block here */&lt;/script&gt; 中并将其放在 HTML 的头部
  • 非常感谢您的耐心等待,@mplungjan!任何提示我如何让 EventListener 对用户输入做出反应,而不是需要点击提交按钮(见上文)。
  • 我添加了第二个示例。我有一种感觉,您可能想要更新 iFrame 而不是重新加载页面
  • 再次感谢您。我会尽快尝试该解决方案并报告!
猜你喜欢
  • 2017-12-20
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 2016-12-04
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
相关资源
最近更新 更多