vue: v-model do not work with hidden input ?
my code:
<input type="hidden" name="measureTime" v-model="measureTime" v-validate="required">
this.measureTime is always empty, but i can see the value in the html source code. html source code:
<input type="hidden" name="measureTime" value="af">
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 5
- Comments: 30 (2 by maintainers)
Commits related to this issue
- parkstay.frontend.exploreparks: fixes, downgrade Vue to avoid https://github.com/vuejs/vue/issues/1194 — committed to dbca-wa/ledger by deleted user 7 years ago
v-model doesn’t work on hidden input because you shouldn’t be using hidden input when you are using Vue. Why store state in the DOM when you can store it in JS directly?
Had an issue with Edge not liking
v-modelon hidden inputs and came across this.I think the intention with
v-modelon hidden inputs is to set up a one-way binding, in which case it’s much better to use<input type="hidden" :value="someData" >@yyx990803 same libraries like semantic-ui require hidden input, e.g to display dropdown with multiples selection.
@rjoo using
:valuenot solve my problem, I use Vue.js 2.0, whats version you are using?I know this issue is very old. But I can’t find any simpler solution that just use the basic layout of semantic ui dropdown. So I just want to share a work around when using Semantic UI dropdown by attaching to change event on the hidden field.
<input type="hidden" v-model="yourModelProperty" @change="yourModelProperty=$event.target.value" >@yyx990803 Hi, can you please explain me why such limitation got to VUE? html DOM itself allows to set values to hidden inputs…
v-modelis meant to provide a two-way binding and react tu user-input.Just use
v-bind:value.But I just want to use the submit event of form
I overcame this problem by using
refon the parent element and then watching for a change either on the hidden field or wait for a callback from the 3rd party lib like semantic ui and then$emitthe new value to the ref. I wrap my 3rd party scripts in components to make them reusable, so here is a quick example of how this would look with semantic ui’s dropdown:after this you can use
v-modelas usual:Works like a charm. VueJs ❤️
const field = document.querySelector(“input[id=trix-input-]”).value I’m using this as a solution.
In your main.js or app.js
You can change the template wrapped in ` ` as much as you want. and in your VueComponent.vue
Ba-Dum-Tss!
Why is it closed?? Vue will not be friends with hidden fields?? 🤔
Trying to have a custom OTC input from design, ran into this issue. I resolved it by setting the CSS to opacity: 0 and making the input absolute so it didn’t take up space in the layout. Display:none and visibility:hidden both prevented v-model binding. I get the philosophy, but seems like having these types of safety just means we have to resort to even trickier solutions.
The core problem is that HTML fields don’t fire a DOM event when JavaScript code changes the value of the field, only when the user interacts with the field. So there’s nothing for Vue to “listen” for that would indicate a change in the value.
This is true of any field modified by third-party code, not just hidden fields, but hidden fields are sort of special because they never emit events (because the user never interacts with them).
There are two ways I’m aware of to work around this (I haven’t used either yet):
Modify the third-party code to, in addition to setting the hidden field value, fire a “change” event (or a synthetic event if using the native one is problematic) on that element. Then I expect you could add a
v-onlistener in Vue like you would to respond to a user’s native input.Have your Vue control set up a
MutationObserver(assuming you don’t need IE10 support) to watch thevalueattribute of the hidden field. Theoretically, you might be able to code this up in the form of a Vue directive so it can be re-used easily.Or… find or write an even better color picker in Vue! 😃
How about 3rd party plugins, like jquery color picker?
@cspeer Semantic UI - 2.2.7
Today I fixed my problem.
I move the following function into my Vue component.
$( document ).ready(function() { $('.ui.dropdown').dropdown({ // dropdown not defined here on: 'hover' }); });