How to get a Object from inputs in vanilla javascript

后端 未结 3 544
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 22:35

For exemple, i have 3 inputs





        
3条回答
  •  不知归路
    2021-01-16 23:35

    Use JavaScript object instead of an array. To create a object key you just have to declare it between the brackets [ ] and assign a value.

    var obj = {};
    var x = document.querySelectorAll('input');
    for (var i = 0; i < x.length; i++) {
      obj[x[i].name] = x[i].value
    }
    console.log(obj);
    
    
    

提交回复
热议问题