本文實(shí)例講述了微信小程序bindinput與bindsubmit的區(qū)別。分享給大家供大家參考,具體如下:
實(shí)際上的話,bindinput還有bindsubmit是沒(méi)有任何的可比性的,但是兩者還是常常被人一起比較,為啥?
比如一個(gè)簡(jiǎn)單的搜索框,你是愿意選擇使用bindinput還是使用bindsubmit?,雖然一個(gè)是表單,一個(gè)是文本輸入框,但是兩者都有一個(gè)共同點(diǎn),就是可以去獲取到用戶的輸入信息。這個(gè)時(shí)候,問(wèn)題來(lái)了,區(qū)別在哪兒?
最簡(jiǎn)單的一段代碼在下面:
<form bindsubmit='input1input'> <input name="input1" placeholder='這個(gè)是輸入框' style='border:1rpx solid #000;'></input> <button form-type="submit">提交按鈕</button></form><input bindinput='input2input' placeholder='這個(gè)是輸入框' style='margin-top:200rpx;border:1rpx solid #000;'></input><button>提交按鈕</button>
// pages/input/input.jsPage({ data: { input1: "", input2: "" }, input1input: function (e) { console.log(e); console.log(e.detail.value.input1) }, input2input: function (e) { console.log(e); console.log(e.detail.value); }})
甚是簡(jiǎn)單,那么問(wèn)題來(lái)了,區(qū)別在哪兒?
當(dāng)然,暫時(shí)的區(qū)別只有一個(gè),就是console.log
比較多,哈哈,開玩笑的。根據(jù)深圳那邊客戶的反應(yīng),說(shuō)是使用input的時(shí)候,移動(dòng)光標(biāo)到一個(gè)地方后,輸入一個(gè)文字,會(huì)重新移動(dòng)光標(biāo)的尾部,當(dāng)然,不是上面的那些代碼了,上面的代碼是沒(méi)問(wèn)題的
有問(wèn)題的是下面的代碼:
// pages/input/input.jsPage({ data: { input1: "this is the value for input1", input2: "this is the value for input2" }, input1input: function (e) { console.log(e); console.log(e.detail.value.input1); this.setData({ input1: e.detail.value.input1 }) }, input2input: function (e) { console.log(e); console.log(e.detail.value); this.setData({ input2: e.detail.value }) }})
<form bindsubmit='input1input'> <input value='{{input1}}' name="input1" placeholder='這個(gè)是輸入框' style='border:1rpx solid #000;height:100rpx;'></input> <button form-type="submit">提交按鈕</button></form><input value="{{input2}}" bindinput='input2input' placeholder='這個(gè)是輸入框' style='height:100rpx;margin-top:200rpx;border:1rpx solid #000;'></input><button>提交按鈕</button>
其實(shí)原因很簡(jiǎn)單,就在一個(gè)地方,就是刷新界面的this.setData
這而,為啥?因?yàn)槟闼⑿潞螅瑅alue值修改,然后呢?
然后頁(yè)面就刷新了,再然后呢?再然后就是因?yàn)槟阍O(shè)置的是光標(biāo)自動(dòng)移動(dòng)到最后一步,所以,最好的話是使用input的時(shí)候要小心點(diǎn)咯,當(dāng)然我不是說(shuō)使用input沒(méi)有使用submit好,只是適應(yīng)場(chǎng)景不同而已,比如沒(méi)必要使用bindinput的時(shí)候去刷新界面,或則說(shuō)我個(gè)人比較偏愛(ài)使用bindsbmit吧,。
希望本文所述對(duì)大家微信小程序開發(fā)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選