ChatGPT解决这个技术问题 Extra ChatGPT

How to fire an event when v-model changes?

I'm trying to fire the foo() function with the @click but as you can see, need press the radio button two times to fire the event correctly . Only catch the value the second time that you press...

I want to fire the event without @click only fire the event when v-model (srStatus) changes.

here is my Fiddle:

http://fiddle.jshell.net/wanxe/vsa46bw8/

fiddle link is no longer working.
If the intent of calling foo() is to make non template changes, use a watcher
This is a great example of why code is required in the question. Please recover it if possible and show it here.
I wish I could retrieve the code for the question, but it was 5 years ago already... luckily we have good answers

A
Antti29

You can actually simplify this by removing the v-on directives:

<input type="radio" name="optionsRadios" id="optionsRadios1" value="1" v-model="srStatus">

And use the watch method to listen for the change:

new Vue ({
    el: "#app",
    data: {
        cases: [
            { name: 'case A', status: '1' },
            { name: 'case B', status: '0' },
            { name: 'case C', status: '1' }
        ],
        activeCases: [],
        srStatus: ''
    },
    watch: {
        srStatus: function(val, oldVal) {
            for (var i = 0; i < this.cases.length; i++) {
                if (this.cases[i].status == val) {
                    this.activeCases.push(this.cases[i]);
                    alert("Fired! " + val);
                }
            }
        }
    }
});

Can you watch automatically any model change ? For example for a form with multiple inputs that all needs to be watched ?
@EricBurel I know this is old, but the answer is no. You can't watch the entire data object all the fields are bound to, you would need to watch each and every single property of that object individually making this approach problematic for big records on forms with a lot of fields.
@EricBurel Actually you can watch nested objects with the 'deep' property set to 'true' --> vuejs.org/v2/api/#watch
p
pherris

This happens because your click handler fires before the value of the radio button changes. You need to listen to the change event instead:

<input 
  type="radio" 
  name="optionsRadios" 
  id="optionsRadios2" 
  value=""
  v-model="srStatus" 
  v-on:change="foo"> //here

Also, make sure you really want to call foo() on ready... seems like maybe you don't actually want to do that.

ready:function(){
    foo();
},

How would you handle Sliders?
where are the available events listed in the documentation. I can't find it?
These are just standard JavaScript events which you can find here: developer.mozilla.org/en-US/docs/Web/Events#Standard_events
Thanks! But Ready:function didn't work for me. Instead, methods:{ foo(){//do something} }
Also note that if you want to register "v-on:change" events on a custom vue component (single file component), "v-on:input" should be used instead.
a
adadion

You should use @input:

<input @input="handleInput" />

@input fires when user changes input value.

@change fires when user changed value and unfocus input (for example clicked somewhere outside)

You can see the difference here: https://jsfiddle.net/posva/oqe9e8pb/


t
tony19

Vue2: if you only want to detect change on input blur (e.g. after press enter or click somewhere else) do (more info here)

<input @change="foo" v-model... >

If you wanna detect single character changes (during user typing) use

<input @keydown="foo" v-model... >

You can also use @keyup and @input events. If you wanna to pass additional parameters use in template e.g. @keyDown="foo($event, param1, param2)". Comparision below (editable version here)

new Vue({ el: "#app", data: { keyDown: { key:null, val: null, model: null, modelCopy: null }, keyUp: { key:null, val: null, model: null, modelCopy: null }, change: { val: null, model: null, modelCopy: null }, input: { val: null, model: null, modelCopy: null }, }, methods: { keyDownFun: function(event){ // type of event: KeyboardEvent console.log(event); this.keyDown.key = event.key; // or event.keyCode this.keyDown.val = event.target.value; // html current input value this.keyDown.modelCopy = this.keyDown.model; // copy of model value at the moment on event handling }, keyUpFun: function(event){ // type of event: KeyboardEvent console.log(event); this.keyUp.key = event.key; // or event.keyCode this.keyUp.val = event.target.value; // html current input value this.keyUp.modelCopy = this.keyUp.model; // copy of model value at the moment on event handling }, changeFun: function(event) { // type of event: Event console.log(event); this.change.val = event.target.value; // html current input value this.change.modelCopy = this.change.model; // copy of model value at the moment on event handling }, inputFun: function(event) { // type of event: Event console.log(event); this.input.val = event.target.value; // html current input value this.input.modelCopy = this.input.model; // copy of model value at the moment on event handling } } }) div { margin-top: 20px; } Type in fields below (to see events details open browser console)


@keyDown (note: model is different than value and modelCopy)
key:{{keyDown.key}}
value: {{ keyDown.val }}
modelCopy: {{keyDown.modelCopy}}
model: {{keyDown.model}}

@keyUp (note: model change value before event occure)
key:{{keyUp.key}}
value: {{ keyUp.val }}
modelCopy: {{keyUp.modelCopy}}
model: {{keyUp.model}}

@change (occures on enter key or focus change (tab, outside mouse click) etc.)
value: {{ change.val }}
modelCopy: {{change.modelCopy}}
model: {{change.model}}

@input
value: {{ input.val }}
modelCopy: {{input.modelCopy}}
model: {{input.model}}


keyDown not working than 'backspace' pressed. So it is better to use @input
In this fiddle we see that backspace works with @keyDown jsfiddle.net/rLzm0f1q (however I don't say that @input is bad or good)
M
Matthew

Just to add to the correct answer above, in Vue.JS v1.0 you can write

<a v-on:click="doSomething">

So in this example it would be

 v-on:change="foo"

See: http://v1.vuejs.org/guide/syntax.html#Arguments