我正在尝试实施评级系统但是我不能在Vue js中传递评级值?

时间:2017-12-07 12:30:36

标签: javascript vue.js vuejs2 vue-component vue-router

我浏览了文档并尝试了以下代码。我能够获取enquiryDesc的数据,但获得评级的空值。我尝试了几个步骤并获得空值。

我的html表单是

<form id="enquiryBox" method="POST" onSubmit="return false;" data-parsley-validate="true" v-on:submit="handelSubmit($event);">
  <div class="modal-body brbottom-20">
    <div class="clearfix">
      <div class="col-lg-6">
        <div class="form-group required">
          <fieldset class="rating">
            <input v-model="rating" type="radio" id="rating" name="rating" v-bind:value="5" ><label v-bind:value="5" class = "full" for="star5" title="Awesome"></label>
               <input v-model="rating" type="radio" id="rating" name="rating" v-bind:value="4" ><label v-bind:value="4" class="half" for="star4half" title="Pretty good"></label>
               </fieldset>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-group required">
          <label>Enquiry</label>
          <textarea placeholder="Write your enquiry here" rows="7" id="enquiryDesc" name="enquiryDesc" class="form-control required" title="Desc" v-model="enquiryDesc" required="required"></textarea>
        </div>
      </div>
    </div>
  </div>
  <div class="">
    <button id="btn-submit-enquiry" class="btn whiteButton" type="submit">Post Enquiry</button>
  </div>
</form>

我的vue js脚本为

enquiryBox = new Vue({
   el: "#enquiryBox",
   data: {
     rating: '',
     enquiryDesc: '',
   },
   methods: {
     handelSubmit: function(e) {
       var vm = this;
       data = {};
       data['rating'] = this.rating;
       data['enquiryDesc'] = this.enquiryDesc;
       console.log(data);

       $.ajax({
         url: 'https://api/post/add_review/',
         data: data,
         type: "POST",
         dataType: 'json',
         success: function(e) {
           if (e.status) {
             alert("Success")

           } else {
             vm.response = e;

             alert(" Failed")
           }
         }
       });
       return false;
     }
   },
 });

我对console.log(data)

的回复
  

{rating:'',enquiryDesc:“cghjl,。/”}

我使用了v-modelv-bind:value,现在我不是价值观。我真的被这些困住了。有没有初始化的东西?请帮助我找到解决方案。我已经完成了几个教程,所有内容都以同样的方式表达。

1 个答案:

答案 0 :(得分:0)

您无法在输入中使用value属性。因为Vue是v-model值的真实来源。只需设置默认的v-model值,将自动检查特定输入。

&#13;
&#13;
new Vue({
  el: '#app',
  data () {
    return {
      rating: 3
    }
  },
  methods: {
    send (e) {
      e.preventDefault()
      console.log(this.rating)
    }
  }
})
&#13;
<div id="app">
  <form action="" method="post" @submit="send">
    <fieldset>
      <input type="radio" name="rating" value="5" v-model="rating">
      <input type="radio" name="rating" value="4" v-model="rating">
      <input type="radio" name="rating" value="3" v-model="rating">
      <input type="radio" name="rating" value="2" v-model="rating">
      <input type="radio" name="rating" value="1" v-model="rating">
    </fieldset>
    <button type="submit">Send</button>
  </form>
</div>

<script src="https://unpkg.com/vue@2.5.9/dist/vue.min.js"></script>
&#13;
&#13;
&#13;

在您的代码中尝试这些更改 - 在HTML中:

<fieldset class="rating">
  <label class="full" for="star5" title="Awesome">5</label>
  <input v-model="rating" type="radio" id="star5" name="rating" value="5">
  <label class="half" for="star4half" title="Pretty good">4</label>
  <input v-model="rating" type="radio" id="star4half" name="rating" value="4">
</fieldset>

在JS中:

enquiryBox = new Vue({
  el: "#enquiryBox",
  data: {
    rating: 5,
    enquiryDesc: '',
  },
  methods: {
    handelSubmit: function (e) {
      const vm = this
      const data = {}

      data['rating'] = this.rating
      data['enquiryDesc'] = this.enquiryDesc

      $.ajax({
        url: 'https://api/post/add_review/',
        data: data,
        type: "POST",
        dataType: 'json',
        success: function (e) {
          if (e.status) {
            alert('Success')
          } else {
            vm.response = e
            alert('Failed')
          }
        }
      })

      return false
    }
  }
})

也改变第一行并尝试agin:

<form id="enquiryBox" method="post" data-parsley-validate="true" @submit="handelSubmit">