?在一個(gè)比如表單驗(yàn)證的過(guò)程中,有時(shí)候,假如要用戶一定要輸入某個(gè)字段,否則不給輸入
其他字段,即屏蔽其他字段的輸入,這個(gè)時(shí)候依然可以用jquery可以做到,例子如下:
1 假如頁(yè)面有三個(gè)字段,用戶名和密碼和密碼確認(rèn)三個(gè)字段:
?
? <form>
<div>
<span class="label">User Name </span>
<input type="text" class="username" name="username"/>
<span class="error"> User name cannot be blank</span>
</div>
<div>
<span class="label">Password </span>
<input type="password" class="password" name="password" />
<span class="error"> Password cannot be blank</span>
</div>
<div>
<span class="label">Confirm Password </span>
<input type="password" class="confpass" name="confpass" />
<span class="error"> Password and Confirm Password don't match</span>
</div>
</form>
2 jquery中的腳本設(shè)置
?
- $(document).ready(function()?{ ??
- $( '.error' ).hide(); ??
- $( '.username' ).blur(function(){ ??
- data=$( '.username' ).val(); ??
- var?len=data.length; ??
- if (len< 1 ) ??
- { ??
- $( '.username' ).next().show(); ??
- $( '.password' ).attr( 'disabled' , true ); ??
- $( '.confpass' ).attr( 'disabled' , true ); ??
- } ??
- else ??
- { ??
- $( '.username' ).next().hide(); ??
- $( '.password' ).removeAttr( 'disabled' ); ??
- $( '.confpass' ).removeAttr( 'disabled' ); ??
- } ??
- }); ??
- $( '.password' ).blur(function(){ ??
- data=$( '.password' ).val(); ??
- var?len=data.length; ??
- if (len< 1 ) ??
- { ??
- $( '.password' ).next().show(); ??
- $( '.confpass' ).attr( 'disabled' , true ); ??
- } ??
- else ??
- { ??
- $( '.password' ).next().hide(); ??
- $( '.confpass' ).removeAttr( 'disabled' ); ??
- } ??
- }); ??
- $( '.confpass' ).blur(function(){ ??
- if ($( '.password' ).val()?!=$( '.confpass' ).val()) ??
- { ??
- $( '.confpass' ).next().show(); ??
- } ??
- else ??
- { ??
- $( '.confpass' ).next().hide(); ??
- } ??
- }); ??
- });??
$(document).ready(function() { $('.error').hide(); $('.username').blur(function(){ data=$('.username').val(); var len=data.length; if(len<1) { $('.username').next().show(); $('.password').attr('disabled',true); $('.confpass').attr('disabled',true); } else { $('.username').next().hide(); $('.password').removeAttr('disabled'); $('.confpass').removeAttr('disabled'); } }); $('.password').blur(function(){ data=$('.password').val(); var len=data.length; if(len<1) { $('.password').next().show(); $('.confpass').attr('disabled',true); } else { $('.password').next().hide(); $('.confpass').removeAttr('disabled'); } }); $('.confpass').blur(function(){ if($('.password').val() !=$('.confpass').val()) { $('.confpass').next().show(); } else { $('.confpass').next().hide(); } }); });
???
原理其實(shí)很簡(jiǎn)單,比如這里,在每個(gè)文本框的onblur事件中進(jìn)行判斷,先看用戶框
中的是否輸入和長(zhǎng)度是否匹配,如果不符合要求的話,則$('.username').next().show();
這句,其實(shí)是顯示下一個(gè)元素(即出錯(cuò)信息提示,即<span class="error"> User name cannot be blank</span>
)
? 并且同時(shí)$('.password').attr('disabled',true);
$('.confpass').attr('disabled',true);
?? 將其他字段的disabled屬性設(shè)置為T(mén)RUE即可.
?? 當(dāng)然,要是輸入了的話,也要重新設(shè)置回來(lái),道理就這么簡(jiǎn)單了
?
?
?
?
轉(zhuǎn)自: http://jackyrong.iteye.com/blog/1148856
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
