输入中的隐藏值不适用于VUE JS

时间:2017-10-23 21:04:21

标签: javascript vue.js

vue隐藏的值不起作用。

一个输入的v模型不起作用。

<template>
  <form class="form-inline" type="POST" @submit.prevent="insertComment" role="form">
    {{ csrf_field() }}
    <div class="form-group">
      <input class="form-control" type="text" name="text" v-model="newcomment.text" placeholder="Your comments" />
      <input type="text" name="post_id" v-model="newcomment.post_id" value="@{{items.id}}" />
    </div>
    <div class="form-group">
      <input type="submit" class="btn btn-default" value="Enviar">
    </div>
  </form>
</template>

v-model="newcomment.post_id"值为null

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

我将假设您使用vue 2,并且您的要求基本上归结为输入的默认值

  1. 内部属性内插已在vuejs 2中删除。因此value="@{{items.id}}"不是法律声明。
  2. v-model已经固有地传递:value。所以再次传递 value="@{{items.id}}"可能会导致意外行为。 this aspect of v-model mechanics of v-model is documented in vue.js documentation

    所以,如上所述,v-model只是语法糖:

    <input v-bind:value="something" v-on:input="something =$event.target.value">

  3. 请参阅您应该使用的模式来实现输入的默认值:

    <template>
      <form class="form-inline" type="POST" @submit.prevent="insertComment" role="form">
        <div class="form-group">
          <input  v-model="newcomment.post_id"/>
        </div>
    
      </form>
    </template>
    
    <script>
    export default {
      data() {
        return {
          newcomment: {
            post_id: 'this is default value'
          }
        }
      }
    }
    </script>
    

答案 1 :(得分:1)

<template>
  <form class="form-inline" type="POST" @submit.prevent="insertComment" role="form">
    {{ csrf_field() }}
    <div class="form-group">
      <input class="form-control" type="text" name="text" v-model="newcomment.text" placeholder="Your comments" />
      <input type="text" name="post_id" v-model="newcomment.post_id"  />
    </div>
    <div class="form-group">
      <input type="submit" class="btn btn-default" value="Enviar">
    </div>
  </form>
</template>

<script>
export default {
  data() {
    return {
        newcomment.post_id:  _this.items.id
    }
  }
}
</script>

v-model和:value都相同。