我试图在路径加载时从缓存加载表单值,因此如果有人从一条路由导航到另一条路径,则不会丢失它们的设置。复选框和文本输入正常工作。只有选择似乎有问题。
以下是其中的要素:
<select id="client" name="client[]" multiple="" v-model="chosen_clients">
<option v-for="client in clients" v-bind:client="client" :value="client.id">@{{ client.name }}</option>
</select>
首先,我检查缓存并更新地址栏:
beforeCreate: function(){
if(sessionStorage.getItem('invoiceable')){
router.push({ path: '/invoiceable?'+sessionStorage.getItem('invoiceable')});
}
},
然后将数据绑定到地址栏:
data: function(){
return {
chosen_clients: this.$route.query['client[]'] ? (Array.isArray(this.$route.query['client[]']) ? this.$route.query['client[]'] : [this.$route.query['client[]']]) : [],
}
},
稍后,在挂载之后,我希望获取数据并更新地址栏,但是有一个问题:
var data = $('#invoiceable-form').serialize();
//This information does not match
console.log(this.chosen_clients); //This is correct
console.log($('#client').val(); //This is empty, even though visually, the select has selected options
最后,$('#client').val()具有正确的值(这意味着明显选择的选项作为序列化表单的一部分出现。我知道这一点,因为我在console.logs上设置了beforeUpdate,在.serialized中不存在值后不到一秒钟,它就会出现,而不会与select进行任何交互。即使我手动设置$('#client').val(2,12);在.serialize()之前,正确的值也不存在。我可以通过手动将数据添加到.serialize的结果中来解决这个问题,但这感觉很麻烦。
发布于 2017-04-14 06:37:43
@Roy J是对的。选择选项尚未加载。
https://stackoverflow.com/questions/43412947
复制