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

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

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

问题

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

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

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

我的实际代码是:

<form action="https://api.whatsapp.com/send?" method="GET" target="_blank" style="text-align: center;">
    <input type="hidden" name="phone" value="56987955586" />
    <input type="hidden" name="" value="" />
    <input type="hidden" name="text" value="" />
    <ul style="list-style: none; padding: 0; display: grid; grid-gap: 10px;">
        <li style="margin-bottom: 10px;">
            <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 1:</span>
            <input type="number" name="producto1" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
        </li>
        <li style="margin-bottom: 10px;">
            <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 2:</span>
            <input type="number" name="producto2" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
        </li>
    </ul>
    <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;">
        Enviar Pedido por WhatsApp
    </button>
</form>
<style>
@media (min-width: 501px) {
    ul {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 801px) {
    ul {
        grid-template-columns: repeat(3, 1fr);
    }
}
</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:

<form action="https://api.whatsapp.com/send?" method="GET" target="_blank" style="text-align: center;">
    <input type="hidden" name="phone" value="56987955586" />
    <input type="hidden" name="" value="" />
    <input type="hidden" name="text" value="" />
    <ul style="list-style: none; padding: 0; display: grid; grid-gap: 10px;">
        <li style="margin-bottom: 10px;">
            <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 1:</span>
            <input type="number" name="producto1" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
        </li>
        <li style="margin-bottom: 10px;">
            <span style="font-weight: bold; display: inline-block; vertical-align: middle;">Producto 2:</span>

            <input type="number" name="producto2" min="0" value="0" style="margin-left: 5px; width: 70px; display: inline-block; vertical-align: middle;" />
        </li>
    </ul>
    <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;">
        Enviar Pedido por WhatsApp
    </button>
</form>
<style>
@media (min-width: 501px) {
    ul {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 801px) {
    ul {
        grid-template-columns: repeat(3, 1fr);
    }
}
</style>

答案1

得分: 1

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

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

<input
  type="number"
  id="producto1"
  min="0"
  value="0"
  class="text-body"
/>

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

<form id="whatsapp-form" ...>
  <!-- ... -->
</form>
<script>
document.getElementById("whatsapp-form").addEventListener("submit", (e) => {
  e.target.querySelector("input[name=text]").value = Array.from(
    e.target.querySelectorAll(".text-body"),
    ({ id, value }) => `${id}=${value}`
  ).join(" ");
});
</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

&lt;input
  type=&quot;number&quot;
  id=&quot;producto1&quot;
  min=&quot;0&quot;
  value=&quot;0&quot;
  class=&quot;text-body&quot;
/&gt;

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

&lt;form id=&quot;whatsapp-form&quot; ...&gt;
  &lt;!-- ... --&gt;
&lt;/form&gt;
&lt;script&gt;
document.getElementById(&quot;whatsapp-form&quot;).addEventListener(&quot;submit&quot;, (e) =&gt; {
  e.target.querySelector(&quot;input[name=text]&quot;).value = Array.from(
    e.target.querySelectorAll(&quot;.text-body&quot;),
    ({ id, value }) =&gt; `${id}=${value}`
  ).join(&quot; &quot;);
});
&lt;/script&gt;

答案2

得分: 0

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

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

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

const form = document.querySelector("form"),
      phone = form.phone.value;

form.addEventListener("submit", e => {
    //1. 阻止表单提交
    e.preventDefault();
    
    //2. 获取 productos 的值
    const values = [...form.querySelectorAll("[name^=producto]")].map(elem => elem.previousElementSibling.textContent + elem.value);
    
    //3. 将这些值设置为消息正文
    const text = `这里是产品: ${values.join(", ")}`;
    
    //4. 构建查询字符串
    const queryString = new URLSearchParams({phone, text});
    
    //5. 构建 URL
    const url = new URL(form.action);

    //6. 设置 URL 的查询字符串
    url.search = queryString;
    
    //7. 发送消息(新标签页)
    window.open(url);

    //可选: location = url; (同一标签页)
}, 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 -->

const form = document.querySelector(&quot;form&quot;),
      phone = form.phone.value;

form.addEventListener(&quot;submit&quot;, e =&gt; {
    //1. Stop form submit
    e.preventDefault();
    
    //2. Get the productos values
    const values = [...form.querySelectorAll(&quot;[name^=producto]&quot;)].map(elem =&gt; elem.previousElementSibling.textContent + elem.value);
	    
    //3. Set those values as the message body
    const text = `Here are the products: ${values.join(&quot;, &quot;)}`;
	    
    //4. Build the query string
    const queryString = new URLSearchParams({phone, text});
	    
    //5. Build the URL
    const url = new URL(form.action);

    //6. Set URL&#39;s query string
    url.search = queryString;
	    
    //7. Send the message (new tab)
    window.open(url);

    //Optional: location = url; (same tab)
}, false);

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

@media (min-width: 501px) {
    ul {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 801px) {
    ul {
        grid-template-columns: repeat(3, 1fr);
    }
}

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

&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;
    &lt;input type=&quot;hidden&quot; name=&quot;phone&quot; value=&quot;56987955586&quot; /&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;&quot; value=&quot;&quot; /&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;text&quot; value=&quot;&quot; /&gt;
    &lt;ul style=&quot;list-style: none; padding: 0; display: grid; grid-gap: 10px;&quot;&gt;
        &lt;li style=&quot;margin-bottom: 10px;&quot;&gt;
            &lt;span style=&quot;font-weight: bold; display: inline-block; vertical-align: middle;&quot;&gt;Producto 1:&lt;/span&gt;
            &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;
        &lt;/li&gt;
        &lt;li style=&quot;margin-bottom: 10px;&quot;&gt;
            &lt;span style=&quot;font-weight: bold; display: inline-block; vertical-align: middle;&quot;&gt;Producto 2:&lt;/span&gt;
            &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;
        &lt;/li&gt;
    &lt;/ul&gt;
    &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;
        Enviar Pedido por WhatsApp
    &lt;/button&gt;
&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:

确定