【问题标题】:How can i get multiple data from multiple tags as store them as json in javascript, jquery我如何从多个标签中获取多个数据,并将它们作为 json 存储在 javascript、jquery 中
【发布时间】:2021-07-31 20:49:15
【问题描述】:
<select class="form-control js-example-basic-single getDress" name="dress[]" id="dress"
                                    style="width: 50%;">
                                <option></option>
                            </select>
                            <input type="text" class="form-control" id="amount" name="amount[]" style="width: 40%;"
                                   placeholder="How many?">
                            <button type="button" class="btn btn-secondary" id="add-more"><i
                                        class="fa fa-plus"></i></button>
                            </div

上面是html

 let dress = document.getElementsByName('dress[]');
    let amount = document.getElementsByName('amount[]');

        let a, b, i;
    for ( i = 0; i < dress.length && amount.length; i++) {
         a = dress[i];
        b = amount[i];
       
    let Dresses = [];
    Dresses[i] = [a.value, b.value];
    console.log(Dresses[i]);


    let k, n = Dresses.length;
    console.log(Dresses.length);

        let tmp = [];
        for (k = 0; k < n; k++) {
            tmp[k] = new DressDetails(Dresses[k][0], Dresses[k][1]);
            console.log(tmp[k]);
        }
        let objJSON = JSON.stringify(tmp);
        console.log(objJSON);

有没有办法将 JSON 响应组合在一起?因为我正在单独收到回复。

它给我的回复是[{"dressType":"T-Shirt","dressAmount":"2"}]。仅适用于 1 个输入。但是如果有多个输入,它会显示这个错误“Uncaught TypeError: Cannot read property '0' of undefined”。

【问题讨论】:

  • 您希望i &lt; dress.length &amp;&amp; amount.length 做什么?你的意思是i &lt; dress.length &amp;&amp; i &lt; amount.length
  • @SebastianSimon。是的,这就是我的意思。但似乎你的更符合逻辑。
  • 语义不同。 i &lt; dress.length &amp;&amp; amount.length 表示 i 小于 dresslength 并且 amountlength 不是 0
  • @SebastianSimon。请进一步解释。
  • 请提供清晰的代码。

标签: javascript jquery json


【解决方案1】:

你想要这样的东西吗?

let dress = [{
  value: 'dress1'
}, {
  value: 'dress2'
}]
let amount = [{
  value: 1
}, {
  value: 2
}]

const result = dress.map((dress, index) => ({
  dress: dress.value,
  price: amount[index].value
}))
console.log(result);

【讨论】:

  • 是的。像这样。我想从每个标签的每个变量中获取多个值
  • 这能回答你的问题吗?如果不是,预期的输出是什么?
  • 这是预期的输出。但它并没有给我同样的输出
  • @bel2atar 上面的 html 是被复制以获得多个 json 响应的内容
【解决方案2】:
let dress = document.getElementsByName('dress[]');
    let amount = document.getElementsByName('amount[]');
    let a, b, i;
    let Dresses = [];
    let tmp = [];
    for (i = 0; i < dress.length && i < amount.length; i++) {
        a = dress[i];
        b = amount[i];
        Dresses[i] = [a.value, b.value];
    }
    Dresses.forEach((Dresses, index) => {
        tmp[index] = new DressDetails(Dresses[0], Dresses[1]);
        // console.log( new DressDetails(Dresses[0], Dresses[1]));
        //  console.log('Index: ' + index + ' Value: ' + Dresses);

    });
    let objJSON = JSON.stringify(tmp);
    console.log(objJSON);

终于搞定了。这是正确的格式。感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 2022-07-27
    • 2023-04-09
    • 2014-06-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多