将自动完成输入值获取到隐藏的输入Vue.Js

时间:2019-06-01 11:18:39

标签: javascript jquery html vue.js

我使用https://santiblanko.github.io/vue-instant/来自动完成Vue。

它可以按我预期的方式工作,除了我无法将“ input”值内的值发布到表单上。

提交表单时,输入中的值不会发布到我的后端服务器上。

所以提交时我必须创建一个隐藏值,以便我可以读取该输入值。

如何执行此操作。我只希望在单击放大镜图标时提交表单和值。

enter image description here

https://codepen.io/snarex/pen/vwQjWR

<link rel="stylesheet" type="text/css" href="https://unpkg.com/vue-instant@0.0.10/dist/vue-instant.css">
<script type="text/javascript" src="https://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript" src="https://unpkg.com/vue-clickaway@2.1.0/dist/vue-clickaway.js"></script>
<script type="text/javascript" src="https://unpkg.com/vue-instant@1.0.4/dist/vue-instant.browser.js"></script>
<script type="text/javascript" src="https://unpkg.com/axios/dist/axios.min.js"></script>

<!-- TODO: Missing CoffeeScript 2 -->

<script type="text/javascript">
  //<![CDATA[
  window.onload = function() {
    console.log(VueInstant)
    new Vue({
      el: '#app',
      data: {
        value: '',
        suggestionAttribute: 'original_title',
        suggestions: [],
        selectedEvent: ""
      },
      methods: {
        clickInput: function() {
          this.selectedEvent = 'click input'
        },
        clickButton: function() {
          this.selectedEvent = 'click button'
        },
        selected: function() {
          this.selectedEvent = 'selection changed'
        },
        enter: function() {
          this.selectedEvent = 'enter'
        },
        keyUp: function() {
          this.selectedEvent = 'keyup pressed'
        },
        keyDown: function() {
          this.selectedEvent = 'keyDown pressed'
        },
        keyRight: function() {
          this.selectedEvent = 'keyRight pressed'
        },
        clear: function() {
          this.selectedEvent = 'clear input'
        },
        escape: function() {
          this.selectedEvent = 'escape'
        },
        changed: function() {
          var that = this
          this.suggestions = []
          axios.get('https://api.themoviedb.org/3/search/movie?api_key=342d3061b70d2747a1e159ae9a7e9a36&query=' + this.value)
            .then(function(response) {
              response.data.results.forEach(function(a) {
                that.suggestions.push(a)
              })
            })
        }
      },
      components: {
        'vue-instant': VueInstant.VueInstant
      }
    })
  }
  //]]>
</script>

<form action="https://www.google.com" method="GET">
  <div id="app">
    <label>{{selectedEvent}}</label>
    <vue-instant :suggestonallwords="true" :suggestion-attribute="suggestionAttribute" v-model="value" :disabled="false" @input="changed" @click-input="clickInput" @click-button="clickButton" @selected="selected" @enter="enter" @key-up="keyUp" @key-down="keyDown"
      @key-right="keyRight" @clear="clear" @escape="escape" :show-autocomplete="true" :autofocus="false" :suggestions="suggestions" name="customName" placeholder="custom placeholder" type="google"></vue-instant>
  </div>
</form>

2 个答案:

答案 0 :(得分:0)

如果我理解正确,是否要将建议值发布到表单数据对象内的服务器?如果是正确的话,您可以按以下方式更改clickButton方法。

clickButton: function() {
      this.selectedEvent = 'click button';
      var formData = new FormData();
      formData.append("selectedMovie", this.value);
      axios({
        method: 'post',
        url: '/yourapi/endpoint/',
        data: {
          formData:formData
        }
      }).then(function(response) {
        console.log(response.data);
        console.log(response.status);
        console.log(response.statusText);
      });
    }

答案 1 :(得分:0)

您可以将以下内容添加到表单元素v-on:submit.prevent="send"的html中。然后将send函数添加到组件的方法中,在这些方法中,您可以访问this.value并执行所需的操作。