我如何在vuejs中使用一个输入来控制另一个

时间:2019-08-08 14:44:28

标签: javascript vue.js inputbinding

如果评估费输入具有键入的值,我需要找到一种方法来基本在“ sampions_period”输入中显示“每月”。

我尝试了绑定和组件,但是无法实现。

            <div class="col-lg-6">
              <div class="form-group">
                <label>Assessment Fee <span class="note">C</span></label>
                <input type="text" class="form-control" v-model="document.assessments_fee" placeholder="$">
              </div>
            </div>
            <div class="col-lg-6">
              <div class="form-group">
                <label>Assessment Period <span class="note">C</span></label>
                <input type="text" class="form-control" v-model="document.assessments_period" placeholder="(per month/quarter/etc.)">
              </div>
            </div>



  data() {
    return {
      now: new Date().toISOString(),
      document: {
        assessments_fee: '',
        assessments_period: '',
      }
    }
  },



      components: {
        assessments_fee: function() {
          if(this.assessments_fee != null || '') this.assessments_period = "per Month";
        }
      },

1 个答案:

答案 0 :(得分:0)

您可以为assessments_fee创建一个观察者,因此当它不是null assessments_period= 'per Month'时。

您可以通过以下方式进行操作:

new Vue({
  el: '#app',
  data() {
    return {
      now: new Date().toISOString(),
      document: {
        assessments_fee: '',
        assessments_period: '',
      }
    }
  },
  computed: {
    assessmentsFee() {
      return this.document.assessments_fee
    }
  },
  watch: {
    assessmentsFee() {
      this.document.assessments_fee ?
      this.document.assessments_period = "per Month" :
      this.document.assessments_period = ""
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="col-lg-6">
    <div class="form-group">
      <label>Assessment Fee <span class="note">C</span></label>
      <input type="text" class="form-control" v-model="document.assessments_fee" placeholder="$">
    </div>
  </div>
  <div class="col-lg-6">
    <div class="form-group">
      <label>Assessment Period <span class="note">C</span></label>
      <input type="text" class="form-control" v-model="document.assessments_period" placeholder="(per month/quarter/etc.)">
    </div>
  </div>
</div>