javascript - Javascript 使用“多个”属性附加输入
问题描述
当输入具有“多个”属性时,如何使用 javascript 附加输入的值?
IE<input type='hidden' name='test[]' multiple>
就像您如何设置普通输入的值一样,document.getElementById("myId").value = "whatever";
但要使用具有“多个”属性的输入
解决方案
我正在尝试设置输入的值,而不是属性。通过设置,我的意思是追加,因为它是一个“多个”输入
该multiple
属性不适用于hidden
输入。
如果要附加到值,则读取当前值,并将其包含在新值中。
input.value = input.value + "some other string";
由于您使用了 PHP 风格的命名约定,因此您可能想要创建一个额外的输入:
const newInput = document.createElement("input");
newInput.name = "test[]";
newInput.type = "hidden";
newInput.value = "some other string";
input.insertAdjacentElement("afterend", newInput);
因此,提交的数据将被视为一个值数组,在提交后处理它的任何内容。
推荐阅读
- sql - 加入 NM 关系表的 SQL 输出
- python - 合并具有不同列名和不同行数的多个数据框
- javascript - 无法读取 Redux 中未定义的属性“类型”
- python - 在 python 中检索词重或 TF_IDF
- javascript - 如何将天气信息从服务器发送到 html 文本区域框?
- discord.js - 在不同的文件中使用客户端
- java - Spring - 当使用带有 json 的 DELETE 时返回错误请求
- css - 基于 Markdown 粗体强调改变 tr 的 CSS 样式
- android - 通知在 Activity 中有效,但在 Service 中无效
- zsh - 在 ZSH 中随机执行命令