在HTML中如何将”&”替换为”%20″。

huangapple go评论109阅读模式
英文:

How do I in replace "&" with "%20" in HTML

问题

最近我创建了一个代码来向WhatsApp发送订单,但在尝试发送消息时遇到了问题。

问题在于,在每个输入之前,消息都会添加一个&,我需要将其替换为%20,以便与WhatsApp一起正常工作。

我已经尝试过更改代码,但似乎没有任何效果。

我的实际代码是:

  1. <form action="https://api.whatsapp.com/send?" method="GET" target="_blank" style="text-align: center;">
  2. <input type="hidden" name="phone" value="56987955586" />
  3. <input type="hidden" name="" value="" />
  4. <input type="hidden" name="text" value="" />
  5. <ul style="list-style: none; padding: 0; display: grid; grid-gap: 10px;">
  6. <li style="margin-bottom: 10px;">
  7. <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 1:</span>
  8. <input type="number" name="producto1" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
  9. </li>
  10. <li style="margin-bottom: 10px;">
  11. <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 2:</span>
  12. <input type="number" name="producto2" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
  13. </li>
  14. </ul>
  15. <button type="submit" style="background-color: #00c853; color: #ffffff; font-size: 20px; border-radius: 40px; border: none; padding: 15px 30px; margin: 20px auto; display: block; text-transform: uppercase; font-weight: bold;">
  16. Enviar Pedido por WhatsApp
  17. </button>
  18. </form>
  19. <style>
  20. @media (min-width: 501px) {
  21. ul {
  22. display: grid;
  23. grid-template-columns: repeat(2, 1fr);
  24. }
  25. }
  26. @media (min-width: 801px) {
  27. ul {
  28. grid-template-columns: repeat(3, 1fr);
  29. }
  30. }
  31. </style>
英文:

I recently created a code to send an order to WhatsApp but I'm running into trouble trying to make the code send a message.

在HTML中如何将”&”替换为”%20″。

The issue is that before each input the message gets an & and I need to replace it with %20 in order for it to work with WhatsApp.

I've tried to change the code and nothing seems to work.

My actual code is:

  1. <form action="https://api.whatsapp.com/send?" method="GET" target="_blank" style="text-align: center;">
  2. <input type="hidden" name="phone" value="56987955586" />
  3. <input type="hidden" name="" value="" />
  4. <input type="hidden" name="text" value="" />
  5. <ul style="list-style: none; padding: 0; display: grid; grid-gap: 10px;">
  6. <li style="margin-bottom: 10px;">
  7. <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 1:</span>
  8. <input type="number" name="producto1" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
  9. </li>
  10. <li style="margin-bottom: 10px;">
  11. <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 2:</span>
  12. <input type="number" name="producto2" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
  13. </li>
  14. </ul>
  15. <button type="submit" style="background-color: #00c853; color: #ffffff; font-size: 20px; border-radius: 40px; border: none; padding: 15px 30px; margin: 20px auto; display: block; text-transform: uppercase; font-weight: bold;">
  16. Enviar Pedido por WhatsApp
  17. </button>
  18. </form>
  19. <style>
  20. @media (min-width: 501px) {
  21. ul {
  22. display: grid;
  23. grid-template-columns: repeat(2, 1fr);
  24. }
  25. }
  26. @media (min-width: 801px) {
  27. ul {
  28. grid-template-columns: repeat(3, 1fr);
  29. }
  30. }
  31. </style>

答案1

得分: 1

如果您想将 producto 值嵌入到 text 参数中,您需要一些 JavaScript 代码来实现。

首先,将 name 属性替换为 id,以便它们不包含在表单数据中,并添加一个类来帮助识别它们。

  1. <input
  2. type="number"
  3. id="producto1"
  4. min="0"
  5. value="0"
  6. class="text-body"
  7. />

然后,添加一个 submit 事件监听器来收集这些值并将它们插入到 text 输入值中。

  1. <form id="whatsapp-form" ...>
  2. <!-- ... -->
  3. </form>
  4. <script>
  5. document.getElementById("whatsapp-form").addEventListener("submit", (e) => {
  6. e.target.querySelector("input[name=text]").value = Array.from(
  7. e.target.querySelectorAll(".text-body"),
  8. ({ id, value }) => `${id}=${value}`
  9. ).join(" ");
  10. });
  11. </script>
英文:

If you want to embed the producto values into the text parameter, you'll need some JS in order to do so.

First, replace the name attributes with id so they're not included in the form data and add a class to help identify them

  1. &lt;input
  2. type=&quot;number&quot;
  3. id=&quot;producto1&quot;
  4. min=&quot;0&quot;
  5. value=&quot;0&quot;
  6. class=&quot;text-body&quot;
  7. /&gt;

Then add a submit event listener to collect the values and insert them into the text input value

  1. &lt;form id=&quot;whatsapp-form&quot; ...&gt;
  2. &lt;!-- ... --&gt;
  3. &lt;/form&gt;
  4. &lt;script&gt;
  5. document.getElementById(&quot;whatsapp-form&quot;).addEventListener(&quot;submit&quot;, (e) =&gt; {
  6. e.target.querySelector(&quot;input[name=text]&quot;).value = Array.from(
  7. e.target.querySelectorAll(&quot;.text-body&quot;),
  8. ({ id, value }) =&gt; `${id}=${value}`
  9. ).join(&quot; &quot;);
  10. });
  11. &lt;/script&gt;

答案2

得分: 0

根据您的评论,您确实希望将这些输入的 producto[N] 值附加到消息正文中。为了实现这一点,您必须按照以下步骤进行操作:

  1. 停止 表单提交。
  2. 获取 producto[N] 值。
  3. 将这些值设置为消息正文。
  4. 构建查询字符串
  5. 构建 URL
  6. 设置 URL 的查询字符串
  7. 发送 消息。

以下是您提供的代码片段:

  1. const form = document.querySelector("form"),
  2. phone = form.phone.value;
  3. form.addEventListener("submit", e => {
  4. //1. 阻止表单提交
  5. e.preventDefault();
  6. //2. 获取 productos 的值
  7. const values = [...form.querySelectorAll("[name^=producto]")].map(elem => elem.previousElementSibling.textContent + elem.value);
  8. //3. 将这些值设置为消息正文
  9. const text = `这里是产品: ${values.join(", ")}`;
  10. //4. 构建查询字符串
  11. const queryString = new URLSearchParams({phone, text});
  12. //5. 构建 URL
  13. const url = new URL(form.action);
  14. //6. 设置 URL 的查询字符串
  15. url.search = queryString;
  16. //7. 发送消息(新标签页)
  17. window.open(url);
  18. //可选: location = url; (同一标签页)
  19. }, false);

希望这有助于您的项目!

英文:

According to your comments, you do want to attach those input producto[N] values to the message body. In order to get that, you must follow these steps:

  1. Stop form submit.
  2. Get the producto[N] values.
  3. Set those values as the message body.
  4. Build the query string.
  5. Build the URL.
  6. Set URL's query string.
  7. Send the message.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const form = document.querySelector(&quot;form&quot;),
  2. phone = form.phone.value;
  3. form.addEventListener(&quot;submit&quot;, e =&gt; {
  4. //1. Stop form submit
  5. e.preventDefault();
  6. //2. Get the productos values
  7. const values = [...form.querySelectorAll(&quot;[name^=producto]&quot;)].map(elem =&gt; elem.previousElementSibling.textContent + elem.value);
  8. //3. Set those values as the message body
  9. const text = `Here are the products: ${values.join(&quot;, &quot;)}`;
  10. //4. Build the query string
  11. const queryString = new URLSearchParams({phone, text});
  12. //5. Build the URL
  13. const url = new URL(form.action);
  14. //6. Set URL&#39;s query string
  15. url.search = queryString;
  16. //7. Send the message (new tab)
  17. window.open(url);
  18. //Optional: location = url; (same tab)
  19. }, false);

<!-- language: lang-css -->

  1. @media (min-width: 501px) {
  2. ul {
  3. display: grid;
  4. grid-template-columns: repeat(2, 1fr);
  5. }
  6. }
  7. @media (min-width: 801px) {
  8. ul {
  9. grid-template-columns: repeat(3, 1fr);
  10. }
  11. }

<!-- language: lang-html -->

  1. &lt;form action=&quot;https://api.whatsapp.com/send?&quot; method=&quot;GET&quot; target=&quot;_blank&quot; style=&quot;text-align: center;&quot;&gt;
  2. &lt;input type=&quot;hidden&quot; name=&quot;phone&quot; value=&quot;56987955586&quot; /&gt;
  3. &lt;input type=&quot;hidden&quot; name=&quot;&quot; value=&quot;&quot; /&gt;
  4. &lt;input type=&quot;hidden&quot; name=&quot;text&quot; value=&quot;&quot; /&gt;
  5. &lt;ul style=&quot;list-style: none; padding: 0; display: grid; grid-gap: 10px;&quot;&gt;
  6. &lt;li style=&quot;margin-bottom: 10px;&quot;&gt;
  7. &lt;span style=&quot;font-weight: bold; display: inline-block; vertical-align: middle;&quot;&gt;Producto 1:&lt;/span&gt;
  8. &lt;input type=&quot;number&quot; name=&quot;producto1&quot; min=&quot;0&quot; value=&quot;0&quot; style=&quot;margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;&quot; /&gt;
  9. &lt;/li&gt;
  10. &lt;li style=&quot;margin-bottom: 10px;&quot;&gt;
  11. &lt;span style=&quot;font-weight: bold; display: inline-block; vertical-align: middle;&quot;&gt;Producto 2:&lt;/span&gt;
  12. &lt;input type=&quot;number&quot; name=&quot;producto2&quot; min=&quot;0&quot; value=&quot;0&quot; style=&quot;margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;&quot; /&gt;
  13. &lt;/li&gt;
  14. &lt;/ul&gt;
  15. &lt;button type=&quot;submit&quot; style=&quot;background-color: #00c853; color: #ffffff; font-size: 20px; border-radius: 40px; border: none; padding: 15px 30px; margin: 20px auto; display: block; text-transform: uppercase; font-weight: bold;&quot;&gt;
  16. Enviar Pedido por WhatsApp
  17. &lt;/button&gt;
  18. &lt;/form&gt;

<!-- end snippet -->


Thanks Sebastian Simon for the API URL suggest.

huangapple
  • 本文由 发表于 2023年6月29日 08:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定