如何在Vue中获取输入字段值 [英] How to get an input field value in vue

查看:847
本文介绍了如何在Vue中获取输入字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是vue的新手,我希望获得一些帮助从输入字段获取值:

I'm new to vue and I would like some help getting a value from an input field:

所以在我的表格中,我有:

So in my form I have:

<input type="hidden" id="groupId" value="1">

使用jQuery,我会这样做:

And with jQuery I would do:

var group_id = $('#groupId').val();

在Vue中,我不知道如何绑定隐藏字段:

In vue I don't know how to bind the hidden field:

<div id="app">
   <input type="text" v-model="groupId"> //Where do I put the value?
</div>

 new Vue({
    el: '#app',
    data: {
        groupId: //What do I put here to get the field's value?
    }

有人可以帮忙吗?

推荐答案

更新为更新:请参阅这个答案.先前更新的答案是错误的.

Update to the update: See this answer. Previously updated answer was wrong.

原始答案:

在Vue中,您不会从视图中获取任何东西,而是将其放入视图中. Vue做到了.您可以在视图模型中进行所有操作,并在视图中进行绑定,以便Vue知道如何对其进行同步.因此,您可以将输入绑定到模型数据项:

In Vue, you don't get things from the view and put things into the view. Vue does that. You do all manipulations in the viewmodel, and make bindings in the view so that Vue knows how to synchronize it. So you'd bind the input to your model data item:

<input type="hidden" id="groupId" v-model="groupId">

并在您的视图模型中设置其值:

and set its value in your viewmodel:

data: {
    groupId: 1
}

这篇关于如何在Vue中获取输入字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆