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í),支持其它類(lèi)型的值,如數(shù)組或?qū)ο蟆?梢酝ㄟ^(guò)下面的教程鏈接查看詳情。
傳送門(mén):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)文章
基于element-ui封裝可搜索的懶加載tree組件的實(shí)現(xiàn)
這篇文章主要介紹了基于element-ui封裝可搜索的懶加載tree組件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Vant彈出列表多選輸入框下拉選擇代碼(可直接復(fù)制使用)
vue項(xiàng)目無(wú)論是用element中的Select選擇器,還是使用公司維護(hù)的組件,都可以輕松實(shí)現(xiàn)單選和多選的需求,這篇文章主要給大家介紹了關(guān)于Vant彈出列表多選輸入框下拉選擇的相關(guān)資料,需要的朋友可以參考下2024-01-01
vue3使用iframe嵌入ureport2設(shè)計(jì)器,解決預(yù)覽時(shí)NullPointerException異常問(wèn)題
這篇文章主要介紹了vue3使用iframe嵌入ureport2設(shè)計(jì)器,解決預(yù)覽時(shí)NullPointerException異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue3實(shí)現(xiàn)下拉選擇框多選功能的方法詳解
在vue的實(shí)際開(kāi)發(fā)過(guò)程中,我們?nèi)绾螌⒁赃x中的值直接渲染到頁(yè)面中,下面這篇文章主要給大家介紹了關(guān)于Vue3實(shí)現(xiàn)下拉選擇框多選功能的相關(guān)資料,需要的朋友可以參考下2023-09-09
Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法淺析
這篇文章主要介紹了Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

