简体   繁体   English

Vue通过隐藏输入形成object

[英]Vue pass hidden inputs to form object

I have a form that has nested v-for loops and inside those v-for loops I want to pass the id's of the properties and the types of houses found in each of those properties.我有一个嵌套 v-for 循环的表单,在这些 v-for 循环中,我想传递属性的 id 和在每个属性中找到的房屋类型。 To achieve this I'm using the hidden input as follows:为了实现这一点,我使用了隐藏输入,如下所示:

<template>
    <div>
    <form @submit.prevent="submit()">
       <div v-for="(property, propIndex) in properties" :key="propIndex">
           {{ property.name }}
          <div v-for="(house_type, typeIndex) in property.house_type.data" :key="typeIndex">
              {{ house_type.type }}<br>
              <input type="text" v-model="rent[propIndex][typeIndex]">Rent<br>
              <input type="text" v-model="house_quantity[propIndex][typeIndex]">How many<br>
              <input type="hidden" ref="property_id" :name="form.property_id"  value="hello"><br>
              <input type="hidden" ref="data" :name="form.house_type_id"  :value="house_type.type"><br>
          </div>
          <br>
       </div>
          <button>Submit</button>
    </form>
    </div>
</template>

I already understand that v-model doesn't work for hidden inputs, that is why I'm using name .我已经明白 v-model 不适用于隐藏输入,这就是我使用name的原因。 How do I pass the values of the hidden inputs to the form object in data below.如何在下面的数据中将隐藏输入的值传递给 object 表格。

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
data() {
    return {
            rent:[{}, {}, {}, {}, {}, {}],
            house_quantity:[{}, {}, {},{}, {}, {},{}, {}, {}],
            form:{
                property_id: [],
                house_type_id: [],
            }
            // form: new Array(7).fill({}),

    }
},
}
</script>

Or how do I get the values of the hidden inputs so that I can submit them alongside the rest of the data.或者我如何获取隐藏输入的值,以便我可以将它们与数据的 rest 一起提交。

methods: {
        async submit(){
            this.form.rent = this.rent
            this.form.house_quantity = this.house_quantity
            await axios.post('/api/landlord/set/up/store/part/3', this.form)
        }
  },

Since a hidden input would not be entered by a user, and would be initialized some other way, you can just create properties on your form backing object for the hidden inputs.由于用户不会输入隐藏输入,并且会以其他方式初始化,因此您可以在表单上为隐藏输入创建支持 object 的属性。

Here is a simplified (no v-for loops) example component.这是一个简化的(无 v-for 循环)示例组件。

<template>
  <div class="hidden-input-substitute">
    <div class="row">
      <div class="col-md-6">
        <form @submit.prevent="submitForm">
          <div class="form-group">
            <label>First name</label>
            <input type="text" class="form-control" v-model="formObject.firstName">
          </div>
          <div class="form-group">
            <label>Last name</label>
            <input type="text" class="form-control" v-model="formObject.lastName">
          </div>
          <button class="btn btn-primary">Submit</button>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'

  export default {
    data() {
      return {
        formObject: {
          firstName: '',
          lastName: '',
          hiddenValueOne: 'hiddenOne',
          hiddenValueTwo: 'hiddenTwo'
        }
      }
    },
    methods: {
      submitForm() {
        console.log(this.formObject);
        axios.post('/path/to/endpoint', this.formObject)
        .then( response => console.log(response) )
        .catch( error => console.log(error) )
      }
    }

  }
</script>

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

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