vue.js input框之間賦值方法
如下所示:
demo.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Index Page</title> </head> <body> <form action="" id="demo"> <input type="text" value="調(diào)試 vuejs 2.0" ref="input1"> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" v-on:click="test1">測(cè)試</a> <br> <span>{{ result1 }}</span> <br> <input type="text" v-model="input2"> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" v-on:click="test2">測(cè)試</a> <br> <span>{{ result2 }}</span> </form> <script src="http://cdn.bootcss.com/vue/2.0.3/vue.min.js"></script> <script type="text/javascript" src="demo.js"></script> </body> </html>
demo.js
new Vue({ el: "#demo", data: { result1: null, result2: null, input2: "" }, methods: { test1: function () { this.result1 = this.$refs.input1.value + " 成功!"; }, test2: function () { this.result2 = this.input2 + " 成功!"; } } })
demo.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Index Page</title> </head> <body> <form action="" id="demo"> <input type="text" value="調(diào)試 vuejs 2.0" ref="input1"> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" v-on:click="test1">測(cè)試</a> <br> <span>{{ result1 }}</span> <br> <input type="text" v-model="input2"> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" v-on:click="test2">測(cè)試</a> <br> <span>{{ result2 }}</span> </form> <script src="http://cdn.bootcss.com/vue/2.0.3/vue.min.js"></script> <script type="text/javascript" src="demo.js"></script> </body> </html>
拓展知識(shí):初試Vue之元素、屬性賦值方法
我們?cè)趘ue中數(shù)據(jù)賦值時(shí),會(huì)很自然而然的想到用“Mustache” 雙大括號(hào)插值法來(lái)賦值
在一個(gè)小案例中,比如給一個(gè)img標(biāo)簽附上鏈接 會(huì)很自然的想到
<img src={{img.path}} />
運(yùn)行時(shí)會(huì)很自然的發(fā)現(xiàn)文件資源請(qǐng)求失敗,究其因很簡(jiǎn)單,因?yàn)閟rc是屬性而不是值,故不可直接src={{ img.path }}
當(dāng)然,Vue的 v-bind 已經(jīng)為我們考慮完備了
官方文檔對(duì)于v-bind的說(shuō)明:動(dòng)態(tài)地綁定一個(gè)或多個(gè)特性,或一個(gè)組件 prop 到表達(dá)式。在綁定 class 或 style 特性時(shí),支持其它類型的值,如數(shù)組或?qū)ο???梢酝ㄟ^(guò)下面的教程鏈接查看詳情。
傳送門:v-bind官方說(shuō)明
注意點(diǎn):在普通文本可直接使用雙大括號(hào)法賦值,但對(duì)于標(biāo)簽的屬性最好還是用v-bind來(lái)賦值,測(cè)了下,input 輸入框的 value 和 placeholder 是可以使用雙大括號(hào)法的。但是為了避免異議,還是遵循官方文檔給的要求,給屬性賦值時(shí)用v-bind吧。
以上這篇vue.js input框之間賦值方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VUE+Element實(shí)現(xiàn)增刪改查的示例源碼
這篇文章主要介紹了VUE+Element實(shí)現(xiàn)增刪改查的示例源碼,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-11-11vue定義在computed的變量無(wú)法更新問(wèn)題及解決
這篇文章主要介紹了vue定義在computed的變量無(wú)法更新問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01element el-table實(shí)現(xiàn)多級(jí)表頭的代碼
多級(jí)表頭的作用與優(yōu)勢(shì),多級(jí)表頭能夠清晰地展示數(shù)據(jù)的層次結(jié)構(gòu),幫助我們更好地理解數(shù)據(jù)之間的關(guān)系,下面通過(guò)本文給大家介紹element el-table實(shí)現(xiàn)多級(jí)表頭的代碼,感興趣的朋友跟隨小編一起看看吧2024-04-04vue訪問(wèn)未定義的路由時(shí)重定向404問(wèn)題
這篇文章主要介紹了vue訪問(wèn)未定義的路由時(shí)重定向404問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue 實(shí)現(xiàn)邊輸入邊搜索功能的實(shí)例講解
今天小編就為大家分享一篇vue 實(shí)現(xiàn)邊輸入邊搜索功能的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09使用vue制作探探滑動(dòng)堆疊組件的實(shí)例代碼
探探的堆疊滑動(dòng)組件起到了關(guān)鍵的作用,下面就來(lái)看看如何用vue寫一個(gè)探探的堆疊組件,感興趣的朋友一起看看吧2018-03-03如何使用vuejs實(shí)現(xiàn)更好的Form validation?
如何使用vuejs實(shí)現(xiàn)更好的Form validation?這篇文章主要介紹了vue-form插件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04