gpt4 book ai didi

php - 表单选择查询输出 - 数组

转载 作者:行者123 更新时间:2023-11-28 04:43:38 24 4
gpt4 key购买 nike

我希望我的选择表单返回这样的查询

http://myexamplesite.com/store/?brand=nike+adidas+woodland

但它返回

http://myexamplesite.com/store/?brand=nike&brand=adidas&brand=woodland

我必须对表单进行什么样的更改

我尝试设置选择 - name="brand[]" name="brand[0]"但它返回

brand%5B0%5D=adidas&brand%5B0%5D=nike

表单选择元素在 for each 循环中

最佳答案

你可以做什么:

  • 创建一个名为brand的隐藏表单输入
  • 将您的选择重命名为其他名称,例如。 品牌选择
  • 使用onsubmit拦截表单提交,构建正确的字符串并将其作为值提供给brand输入
  • 禁用 brand-select 输入,这样它就不会发送到服务器
  • 最后提交表单

我在 codepen 上做了一个简单的例子:

HTML:

<form method="get" id="form" action="https://postman-echo.com/get">
<select name="brand-select" id="brand-select" multiple>
<option value="nike">Nike</option>
<option value="adidas">Adidas</option>
<option value="woodland">woodland</option>
</select>
<input type="hidden" name="brand" id="brand">
<input type="submit"></input>
</form>

JS:

const form = document.getElementById('form');
const brandSelector = document.getElementById('brand-select');
const brandInput = document.getElementById('brand');

form.onsubmit = (e) => {
// prevent form from submitting
e.preventDefault();
const formData = new FormData(form);
// get all selected brands from the brand selector
// and join them with '+'
const brands = formData.getAll('brand-select').join('+');
brandInput.value = brands;
// disable brand selector so it;s not included in the params
brandSelector.disabled = true;
// finally, submit the form
form.submit();
};

当您在 Codepen 上提交表单时,您会看到 postman-echo 显示提交的参数:"args":{"brand":"nike+adidas+woodland"}。您还可以使用 devtools 中的 Network 选项卡来检查提交到的 url。在这种情况下是:https://postman-echo.com/get?brand=nike%2Badidas%2Bwoodland 所以 brand 的 urldecoded 值为 nike+adidas+woodland

关于php - 表单选择查询输出 - 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58668459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com