最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue獲取input值的三種常用寫法

 更新時(shí)間:2022年08月10日 09:55:24   作者:June.1  
這篇文章主要介紹了vue獲取input值的三種常用寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1. v-model 表單輸入綁定

使用v-model創(chuàng)建雙向數(shù)據(jù)綁定, 用來監(jiān)聽用戶的輸入事件以更新數(shù)據(jù),并對(duì)一些極端場景進(jìn)行一些特殊處理。

?? ?<template>
?? ? ? ?<div>
?? ? ? ? ? ?<input class="login-input" type="text" ?v-model="username" placeholder="請(qǐng)輸入賬號(hào)">
?? ? ? ? ? ?<input class="login-input" type="password" v-model="password" placeholder="請(qǐng)輸入密碼">
?? ??? ??? ?<div class="login-button" @click="login" type="submit">登陸</div>
?? ? ? ?</div>
?? ?</template>
?? ?<script>
?? ?export default {
? ? ? ?name: 'Login',
? ? ? ? data() {
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? username: '',
? ? ? ? ? ? ? ? password: ''
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? methods: {
? ? ? ? ? ? login() {
? ? ? ? ? ? ? ? ? ?console.log(this.username)
? ? ? ? ? ? ? ? ? ?console.log(this.password)
? ? ? ? ? ? }
? ? ? ? }
? ? }
?? ?<script/>

    

2. @input 監(jiān)聽輸入框

輸入框只要輸入的值變化了就會(huì)觸發(fā) input 調(diào)用 search 數(shù)據(jù)實(shí)時(shí)獲取通過 event.currentTarget.value 獲取到

?? ?<template>
?? ? ?<div class="class">
?? ? ? ?<div>
?? ? ? ? ?<input type="text" @keyup.enter="search" @input="search($event)"/>
?? ? ? ?</div>
?? ? ?</div>
?? ?</template>
?? ?<script>
? ? export default {
? ? ? name: "search",
? ? ? data() {
? ? ? },
? ? ? methods: {
?? ? ? ? ? ?search(event){
?? ? ? ? ? ? ?console.log(event.currentTarget.value)
?? ? ? ? ? ?}
? ? ? ?? ?}
? ? }
? ?</script>

3. ref 獲取數(shù)據(jù)

這種方式類似于原生DOM,但是ref獲取數(shù)據(jù)更方便

?? ?<template>
?? ? ?<div class="class">
?? ? ? ? ?<input type="text" ref="getValue" />
?? ? ? ? ?<button @click="subbmitButton">獲取表單數(shù)據(jù)</button>
?? ? ?</div>
?? ?</template>
?? ?<script>
? ? export default {
? ? ? name: "page",
? ? ? data() {
? ? ? },
? ? ? methods: {
?? ? ? ? ? ?subbmitButton(){
?? ? ? ? ? ? ?console.log(this.$refs.getValue.value)
?? ? ? ? ? ?}
? ? ? ?? ?}
? ? }
? </script>

vue收集input[type=“checkbox”]的值

input[type=“checkbox”],勾選or不勾選

要控制input[type=“checkbox”]勾選或不勾選,有以下兩種方式,

  • v-model="isDone"。isDone為true時(shí),勾選;isDone為false時(shí),不勾選
  • :checked="isDone"。isDone為true時(shí),勾選,isDone為false時(shí),不勾選

注意哈?。〈藭r(shí)isDone必須初始化為布爾值(或者可布爾化的類型,如字符串,數(shù)值,非0即1)

v-model

<body>
    <div id="root">
        <input type="checkbox" v-model="isDone"/>已經(jīng)完成
        <button @click="handleClick">勾選/去勾選</button>
    </div>

    <script>
    Vue.config.productionTip = false;
    new Vue({
        el:"#root",
        data(){
            return {
                isDone:false
            }
        },
        methods: {
            handleClick(){
                this.isDone = !this.isDone;
            }
        },
        watch:{
            isDone:{
                immediate:true,
                handler(newValue,oldValue){
                    console.log(newValue,oldValue);
                }
            }
        }
    })
    </script>
</body>

:checked

<body>
    <div id="root">
        <input type="checkbox" :checked="isDone"/>已經(jīng)完成
        <button @click="handleClick">勾選/去勾選</button>
    </div>

    <script>
    Vue.config.productionTip = false;
    new Vue({
        el:"#root",
        data(){
            return {
                isDone:false
            }
        },
        methods: {
            handleClick(){
                this.isDone = !this.isDone;
            }
        },
        watch:{
            isDone:{
                immediate:true,
                handler(newValue,oldValue){
                    console.log(newValue,oldValue);
                }
            }
        }
    })
    </script>
</body>

input[type=“checkbox”]多個(gè)時(shí),哪些被選中

多個(gè)input[type="checkbox"]時(shí),想知道哪些被選中,使用v-model,如v-model="hobbies"。

注意哈!!此時(shí)hobbies必須初始化為數(shù)組。

<body>
    <div id="root">
        <label><input type="checkbox" name="hobby" value="football" v-model="hobbies"/>足球</label><br/>
        <label><input type="checkbox" name="hobby" value="basketball" v-model="hobbies"/>籃球</label><br/>
        <label><input type="checkbox" name="hobby" value="tennis" v-model="hobbies"/>網(wǎng)球</label>
    </div>

    <script>
    Vue.config.productionTip = false;
    new Vue({
        el:"#root",
        data(){
            return {
                hobbies:[]
            }
        },
        watch:{
            hobbies:{
                immediate:true,
                handler(newValue){
                    console.log(newValue);
                }
            }
        }
    })
    </script>
</body>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

古蔺县| 灯塔市| 正宁县| 桂平市| 庆城县| 蓝田县| 灵丘县| 稷山县| 麻阳| 河间市| 虹口区| 县级市| 抚远县| 曲周县| 陇川县| 兴安盟| 墨竹工卡县| 广宁县| 连城县| 永定县| 邢台县| 上蔡县| 长治县| 田林县| 杭锦旗| 文昌市| 商都县| 龙门县| 阿克陶县| 宁南县| 西青区| 海淀区| 甘泉县| 信阳市| 财经| 叙永县| 板桥市| 赣州市| 孝义市| 瓦房店市| 南雄市|