简体   繁体   English

设置VueJS模型属性不会更新DOM中的select元素

[英]Setting VueJS model property doesn't update select element in DOM

I have a cascading dropdown using VueJS (1.0), and I'm having a problem where a change in the Vue model isn't being reflected in the DOM. 我有一个使用VueJS(1.0)的级联下拉列表,我遇到了一个问题,即Vue模型中的更改没有反映在DOM中。

The elements in the dropdown need to be an object, but once selected I'm trying to change the value of the property/dropdown to an int. 下拉列表中的元素需要是一个对象,但一旦选中,我就会尝试将属性/下拉列表的值更改为int。

I'm doing this with a watch event, which processes the necessary information from the object, then uses $set on the property to set it to the required int. 我正在使用监视事件执行此操作,该事件处理来自对象的必要信息,然后使用属性上的$set将其设置为所需的int。

Using the VueJS Chrome dev tools, I can see the change reflected on the component's property, but when submitting the form it's POSTed as the string [object Object] , as if the DOM was never updated. 使用VueJS Chrome开发工具,我可以看到组件属性上反映的更改,但是在提交表单时,它将作为字符串[object Object] POST,就像DOM从未更新过一样。

Here is the relevant dropdown in the template: 以下是模板中的相关下拉列表:

<select :disabled="releases.length &lt; 1" v-model="release" options="releases" class="form-control input-sm" name="{{formname}}[release_id]">
    <option selected="selected" value="">Choose Release...</option>
    <option v-for="obj in releases" v-bind:value="obj">{{obj.text}}</option>
</select>

And here is the watch event: 以下是观看活动:

"release": function() {
   this.$parent.$data.promos = this.release.promos;
   this.$set('release', this.release.id);
}

After changing the dropdown, the root promos property is updated, and according to dev tools the release property of the component is correctly set to the id 更改下拉列表后,将更新根promos属性,并根据dev工具将组件的release属性正确设置为id

开发工具截图

But when the form is submitted, I just get the string representation of the object! 但是当提交表单时,我只是获取对象的字符串表示!

POST截图

Does anyone know what I'm doing wrong here; 有谁知道我在这里做错了什么; or is this a VueJS bug/is there a workaround? 或者这是一个VueJS错误/是否有解决方法?

Thank you! 谢谢!

The value attribute of an option can be tied to an object in Vue, but the browser still needs to render the value as valid HTML so the object is cast to a string. 选项的value属性可以绑定到Vue中的对象,但浏览器仍然需要将值呈现为有效的HTML,以便将对象强制转换为字符串。

One thing you can do is add a hidden field to the form with a value set to the ID of the selected release object. 您可以做的一件事是向表单添加一个隐藏字段,其值设置为所选发布对象的ID。


Edit : Another option could be overriding the toString method of the class prototype to return the ID of the object. 编辑 :另一个选项可能是覆盖类原型的toString方法以返回对象的ID。

I will not be able to find the bug in your code unless I can play around with it in jsFiddle or equivalent. 除非我可以在jsFiddle或同等版本中使用它,否则我将无法在您的代码中找到该错误。

But I have an alternate implementation of cascading dropdown for you here: https://jsfiddle.net/mani04/Lgxrcc5p/1/ 但是我在这里有一个级联下拉列表的替代实现: https//jsfiddle.net/mani04/Lgxrcc5p/1/

You may see if that provides any pointers. 你可能会看到它是否提供任何指针。 This example uses Vue 2.0.3 此示例使用Vue 2.0.3

In your code sample above, I specifically do not understand this part: 在上面的代码示例中,我特别不理解这部分:

<select :disabled="releases.length &lt; 1" ...

Is that a copy-paste error? 这是复制粘贴错误吗? I hope you have the following code in your editor: 我希望你的编辑器中有以下代码:

<select :disabled="releases.length < 1" ...

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

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