我希望能够进行ajax调用并使用返回的结果来使用vue.js生成下拉列表的选项。 我可以这样做:从ajax调用填充Vue.js中的下拉菜单
<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)
}
});
}
我在vue.js真正的新,我将不胜感激,如果有人可以提供帮助。
到目前为止问题是什么?我在你的代码中看到的唯一问题是'this'在你的'success'回调中不会是Vue。 – Bert
我的问题是,我不知道如何将ajax调用的结果附加到vue对象的options属性。如果你在我上面的例子中看到了,我已经对这些选项进行了硬编码(文本:'One',值:'A',文本:'Two',值:'B'等等。) – DoArNa
你说对象降下来作为{id,value}。他们是否需要{文字,价值}? – Bert