简体   繁体   English

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

[英]I am trying to implement a rating system But I am not not able to pass the values of rating in Vue js?

I gone through the documentation and I tried the following code. 我仔细阅读了文档,并尝试了以下代码。 I am able to get the data for enquiryDesc but getting null values for rating. 我能够获取enquiryDesc的数据,但获得的评级为空值。 I tried several steps and getting null values. 我尝试了几个步骤并获取了空值。

My html form is 我的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>

My vue js script is as 我的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;
     }
   },
 });

My response for console.log(data) 我对console.log(data)答复

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

I have used v-model and v-bind:value and now to I am not the values. 我已经使用了v-modelv-bind:value ,现在我不是值。 I am really stuck on these. 我真的坚持这些。 Is something not initialized? 是否未初始化? Please help me to have a solution.I have gone through several tutorials and everything says to do in the same way. 请帮助我找到解决方案。我已经完成了数个教程,所有内容都以相同的方式进行。

You cant use the value attribute in inputs. 您不能在输入中使用value属性。 For Vue is source of truth the v-model value. 因为Vue是真理的来源,所以v模型的价值。 Just set the default v-model value and particular input will be check automatically. 只需设置默认的v-model值,就会自动检查特定的输入。

 new Vue({ el: '#app', data () { return { rating: 3 } }, methods: { send (e) { e.preventDefault() console.log(this.rating) } } }) 
 <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> 

Try these changes in your code - in HTML: 在代码中尝试这些更改-以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>

And in JS: 在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
    }
  }
})

Change the first line also and try it agin: 也更改第一行,然后重新尝试:

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

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

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