简体   繁体   English

Vuejs:如何 console.log() 所有表单输入?

[英]Vuejs : How can I console.log() all form inputs?

I oftenly need to debug my form inputs我经常需要调试我的表单输入

I hate having to do this huge & long and sometimes missing vars...我讨厌不得不做这个巨大而漫长的,有时甚至缺少变量......

console.log(this.type, this.name, this.url, this.timezone, this.startDate, this.endDate, this.startTime, this.endTime, and 10 more variables .... )

Is there a way to console.log(ALL inputs in a form)?有没有办法 console.log(表格中的所有输入)?

I've tried我试过了

getAllData(id) {
        let myForm = document.getElementById(id)
        let formData = new FormData(myForm)
        const data = {}
        // need to convert it before using not with XMLHttpRequest
        for (let [key, val] of formData.entries()) {
            Object.assign(data, { [key]: val })
        }
        console.log(data)
    },

and call it like so:并这样称呼它:

this.getAllData('form')

I got this in my console我在我的控制台里得到了这个

在此处输入图像描述

I can't make sense from any of it...我无法理解它的任何意义...


Try # 2试试#2

I've tried我试过了

getAllData(id) {
    let myForm = document.getElementById(id)
    console.log(
        Array.from(myForm.elements).map((e) => {
            return e.value
        })
    )
},

I get better result now, but I would like to see the key along with the value.我现在得到了更好的结果,但我想看到关键和价值。

This is what I see now.. (only the value)这就是我现在看到的......(只有价值)

在此处输入图像描述

How about scoping your form values in a form value, ie:如何在表单值中限定form值,即:

data: () => ({
  form: {
    errors: {},
    values: {
      type: '', 
      name: ''
    }
  }
})

You can then simply do console.log(this.form) , or plop <pre>{{ form }}</pre> in the template for a JSON output.然后,您可以简单地执行console.log(this.form) ,或在模板中添加<pre>{{ form }}</pre>以获取 JSON output。

Additionally, if you add values and errors within the form value you can do your validation then contain things when outputting, ie此外,如果您在form值中添加valueserrors ,您可以进行验证,然后在输出时包含内容,即

<div class="form-group">
  <label for="input-type">Type</label>
  <input id="input-type" v-model="form.values.type" type="text" :class="['form-control', { 'is-invalid': form.errors.type }]" placeholder="Enter the type of the thing...">
  <div v-if="form.errors.type" class="invalid-feedback">
    {{ form.errors.type }}
  </div>
</div>

You can also use vanilla JS for that, let's say you have the following HTML:您也可以为此使用香草 JS,假设您有以下 HTML:

  <form id="myform">
    <input name="input-1" type="text"/>
    <input name="input-2" type="text"/>
    <button type="submit">Hello</button>
  </form>

and then plain JS然后是普通的 JS

var myForm = document.getElementById('myform');

myForm.addEventListener('submit', (event) => {
  event.preventDefault();
  console.log(Array.from(myForm.elements).map((el) => {
    return `${el.name}: ${el.value}`; // if you have name attribute on inputs
  }))
});

Example on jsbin: https://jsbin.com/tixiduzuyi/edit?html,js,console,output jsbin 上的示例: https://jsbin.com/tixiduzuyi/edit?html,js,console,output

Solution based on HTMLFormElement.elements , so if you have a <form> element you could have access to all your .elements whiting that form, you can also filter by input type, in order to exclude buttons or checkboxes (if needed).基于HTMLFormElement.elements的解决方案,因此,如果您有一个<form>元素,您可以访问包含该表单的所有.elements ,您还可以按input类型进行过滤,以排除按钮或复选框(如果需要)。

I find it easiest to spread (using the ... operator) the form's entries into a console.log() call, which creates an array of key-value pairs for the entries:我发现将表单的entries 传播(使用...运算符)到console.log()调用中最容易,它为条目创建一个键值对数组:

const formData = new FormData(formElement)
console.log(...formData.entries())
             👆

This yields a single line in Chrome's console like this:这会在 Chrome 的控制台中生成一行,如下所示:

截屏

...and you can expand each entry if needed: ...如果需要,您可以扩展每个条目:

屏幕截图 展开

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM