jni*_*eto 76 javascript events vue.js
我正在尝试使用该foo()功能,@click但正如您所见,需要按两次单选按钮才能正确触发事件.只有第二次按下时才能捕捉到该值...
我希望@click在v-model(srStatus)更改时触发事件而不仅仅触发事件.
这是我的小提琴:
小智 80
您可以通过删除v-on指令来实际简化:
<input type="radio" name="optionsRadios" id="optionsRadios1" value="1" v-model="srStatus">
Run Code Online (Sandbox Code Playgroud)
并使用该watch方法来监听更改:
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);
}
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
phe*_*ris 64
发生这种情况是因为您的click处理程序在单选按钮的值更改之前触发.你需要听取这个change事件:
<input
type="radio"
name="optionsRadios"
id="optionsRadios2"
value=""
v-model="srStatus"
v-on:change="foo"> //here
Run Code Online (Sandbox Code Playgroud)
此外,确保你真的想要foo()准备就绪......看起来也许你真的不想这样做.
ready:function(){
foo();
},
Run Code Online (Sandbox Code Playgroud)
Kam*_*ski 21
Vue2:如果你只想检测输入模糊的变化(例如在按下回车或点击其他地方之后)(这里有更多信息)
<input @change="foo" v-model... >
Run Code Online (Sandbox Code Playgroud)
如果您想检测单个字符更改(在用户输入期间)使用
<input @keydown="foo" v-model... >
Run Code Online (Sandbox Code Playgroud)
您还可以使用@keyup和@input事件.如果你想在模板中使用其他参数,例如 @keyDown="foo($event, param1, param2)".下面的比较(此处可编辑版本)
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
}
}
})Run Code Online (Sandbox Code Playgroud)
div {
margin-top: 20px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
Type in fields below (to see events details open browser console)
<div id="app">
<div><input type="text" @keyDown="keyDownFun" v-model="keyDown.model"><br> @keyDown (note: model is different than value and modelCopy)<br> key:{{keyDown.key}}<br> value: {{ keyDown.val }}<br> modelCopy: {{keyDown.modelCopy}}<br> model: {{keyDown.model}}</div>
<div><input type="text" @keyUp="keyUpFun" v-model="keyUp.model"><br> @keyUp (note: model change value before event occure) <br> key:{{keyUp.key}}<br> value: {{ keyUp.val }}<br> modelCopy: {{keyUp.modelCopy}}<br> model: {{keyUp.model}}</div>
<div><input type="text" @change="changeFun" v-model="change.model"><br> @change (occures on enter key or focus change (tab, outside mouse click) etc.)<br> value: {{ change.val }}<br> modelCopy: {{change.modelCopy}}<br> model: {{change.model}}</div>
<div><input type="text" @input="inputFun" v-model="input.model"><br> @input<br> value: {{ input.val }}<br> modelCopy: {{input.modelCopy}}<br> model: {{input.model}}</div>
</div>Run Code Online (Sandbox Code Playgroud)
Per*_*z30 10
您应该使用@input:
<input @input="handleInput" />
Run Code Online (Sandbox Code Playgroud)
@input 用户更改输入值时触发。
@change 当用户更改值和未聚焦输入(例如在外部某处单击)时触发
您可以在这里看到区别:https : //jsfiddle.net/posva/oqe9e8pb/
只是为了添加上面的正确答案,在Vue.JS v1.0中你可以写
<a v-on:click="doSomething">
Run Code Online (Sandbox Code Playgroud)
所以在这个例子中它会是
v-on:change="foo"
Run Code Online (Sandbox Code Playgroud)
请参阅:http://v1.vuejs.org/guide/syntax.html#Arguments
| 归档时间: |
|
| 查看次数: |
128069 次 |
| 最近记录: |