I want to be able to make an ajax call and to use the results returned to generate the options of a drop-down using . I can do this:

<select v-model="selected">
  <option v-for="option in options" v-bind:value="option.value">
    {{ option.text }}
  </option>
</select>
<span>Selected: {{ selected }}</span>

.js文件

new Vue({
  el: '...',
  data: {
    selected: 'A',
    options: [
      { text: 'One', value: 'A' },
      { text: 'Two', value: 'B' },
      { text: 'Three', value: 'C' }
    ]
  }
})

但我不想让我的选项硬编码,而是来自ajax调用.

Ajax调用如下所示:

function pullEmployees(){
        var eventOwnerId = $('#eventOwner').val();
        var eventOwnerName = $('#eventOwner :selected').text();
        $.ajax({
            url: "employees.cfm",
            data: {
                eventOwnerId: eventOwnerId,
                eventOwnerName: eventOwnerName,
                method : "updateOwners"
            },

            success: function(results) {
              // results will have a list of objects where each objects has     two properties (ID and Value)
            }
    });
}

I am really new in and I would appreciate if someone can help.

推荐答案

在下面的示例中,我用setTimeout模拟您的ajax调用.基本上,您希望在一个变量中捕获new Vue()的结果,然后用ajax调用的结果设置该Vue的options属性.

我还更新了你的模板,以反映你返回的选项具有 struct {ID, text}.

console.clear()

let app = new Vue({
  el: '#app',
  data: {
    selected: 'A',
    options: []
  }
})


function pullEmployees(){
  let options = [
    { text: 'One', ID: 'A' },
    { text: 'Two', ID: 'B' },
    { text: 'Three', ID: 'C' }
  ];
  setTimeout(() => app.options = options, 1000)
}
pullEmployees()
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="app">
  <select v-model="selected">
    <option v-for="option in options" v-bind:value="option.ID">
      {{ option.text }}
    </option>
  </select>
  <span>Selected: {{ selected }}</span>
</div>

然而,所有这些都可以在Vue内部完成.不需要在外面做.

let app = new Vue({
  el: '#app',
  data: {
    selected: 'A',
    options: []
  },
  methods:{
    pullEmployees(){
      var eventOwnerId = $('#eventOwner').val();
      var eventOwnerName = $('#eventOwner :selected').text();
      $.ajax({
        url: "employees.cfm",
        data: {
          eventOwnerId: eventOwnerId,
          eventOwnerName: eventOwnerName,
          method : "updateOwners"
        },
        success: results => this.options = results
      });
    }
  },
  mounted(){
    this.pullEmployees()
  }
})

如果eventOwner也是Vue的一部分,您可以从Vue获取该值作为数据属性.

Vue.js相关问答推荐

在VueJS中卸载元素之前是否应该删除JavaScript事件收件箱?

为什么在Vue.js中修改非react 性似乎是react 性的?

为什么元素会从Vue 3中的TransitionGroup左上角出现?

vue3 输入标签给出错误 Vue.use 不是函数

在组件之间共享 websocket 连接

VueJS - 将单独的组件加载到 vue 实例中

VueJS 插件的如何react式绑定?

正确实现 Vue.js + DataTables

vuex 中 { dispatch, commit } 的类型是什么?

Polyfill for ie

如何在 Vue.js 中传递 $router.push 中的数据?

单击复选框后,V-model 未更新

Vue:从 Vue 实例更新组件数据

Javascript/vue.js 接收json

简单的点击功能不触发

Vue.js webpack:如何获取目录中的文件列表?

如何从方法内部访问数据

使用 axios 和 vue.js 取消先前的请求

v-for 中的计算(Computed)/动态(Dynamic) v-model 名称

Vue过滤器应该如何使用typescript绑定?