UserNameUsernamec" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

jquery中限制部分字段不能輸入

系統(tǒng) 2854 0

?在一個(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è)置
?

Java代碼 復(fù)制代碼 ? 收藏代碼
  1. $(document).ready(function()?{ ??
  2. $( '.error' ).hide(); ??
  3. $( '.username' ).blur(function(){ ??
  4. data=$( '.username' ).val(); ??
  5. var?len=data.length; ??
  6. if (len< 1 ) ??
  7. { ??
  8. $( '.username' ).next().show(); ??
  9. $( '.password' ).attr( 'disabled' , true ); ??
  10. $( '.confpass' ).attr( 'disabled' , true ); ??
  11. } ??
  12. else ??
  13. { ??
  14. $( '.username' ).next().hide(); ??
  15. $( '.password' ).removeAttr( 'disabled' ); ??
  16. $( '.confpass' ).removeAttr( 'disabled' ); ??
  17. } ??
  18. }); ??
  19. $( '.password' ).blur(function(){ ??
  20. data=$( '.password' ).val(); ??
  21. var?len=data.length; ??
  22. if (len< 1 ) ??
  23. { ??
  24. $( '.password' ).next().show(); ??
  25. $( '.confpass' ).attr( 'disabled' , true ); ??
  26. } ??
  27. else ??
  28. { ??
  29. $( '.password' ).next().hide(); ??
  30. $( '.confpass' ).removeAttr( 'disabled' ); ??
  31. } ??
  32. }); ??
  33. $( '.confpass' ).blur(function(){ ??
  34. if ($( '.password' ).val()?!=$( '.confpass' ).val()) ??
  35. { ??
  36. $( '.confpass' ).next().show(); ??
  37. } ??
  38. else ??
  39. { ??
  40. $( '.confpass' ).next().hide(); ??
  41. } ??
  42. }); ??
  43. });??
    $(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

jquery中限制部分字段不能輸入


更多文章、技術(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ì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 芷江| 醴陵市| 乐平市| 乌海市| 罗江县| 喀什市| 深州市| 阿勒泰市| 纳雍县| 怀化市| 朝阳区| 达尔| 夏津县| 扎赉特旗| 临桂县| 马关县| 古交市| 江西省| 共和县| 东平县| 平顶山市| 沛县| 乌兰察布市| 冷水江市| 平武县| 六盘水市| 普格县| 恭城| 剑河县| 日土县| 保靖县| 辽阳县| 巴里| 扎鲁特旗| 视频| 喀什市| 莱西市| 东海县| 台北县| 泰和县| 建水县|