20101221 学习记录:jquery按钮的失效与启用


之前用jquery写了个function用来给textbox的输入内容加密的,用失去焦点blur()来触发加密。其中,加密是把输入内容发送到某webservice,返回值即为加密后的值,然后再把加密的值赋给输入框。

但是因为ws是远程的,每次加密完成与鼠标离开输入框的时间多少会有一点时间差,测试的时候发现,如果修改了textbox内的值后,鼠标不离开输入框立刻迅速的去点提交,会出现输入框内容没有被加密就被提交的情况,想了一下,给textbox加了一个focus的事件。在输入框一得到焦点时就disable掉提交按钮,当鼠标焦点离开输入框时,再恢复按钮的可用性。基本语句如下:

 

$(”#test”).attr(’disabled’,false);

$(”#test”).attr(’disabled’,true);
 

也看到有人用下面的方法,没有尝试,Mark一下~

jQuery(this).attr({"disabled":"disabled"});

jQuery("#test").removeAttr("disabled");

 

 

于是删掉了一点细节,其大致的代码如下~

 

ExpandedBlockStart.gif JS代码
$( ' document ' ).ready( function ()

    
// //once the textbox get the focus, then disable the button
    $( ' #ccno ' ).focus( function  ()
    {
        $(
" #submit " ).attr( ' disabled ' , true );
    });
    
    $(
' #ccno ' ).blur( function  ()
    {    
        
if ($( " #ptypeh " ).val() == 1 )
        {
            flag 
=  validateCCNumber(ccno);
            
if (flag)
            {     
                $.ajax({
                type: 
" POST " ,
                url: 
" Ws/xxx.asmx/xxMethod " ,
                data: {ccno: _ccno,group_code:_group},
                dataType: 
" xml " ,
                error: 
function (){
                    alert(
' Error loading XML document ' );
                },
                success: 
function  (xml)  // parseXml
                {  
                    
if  ($(xml).find( " status " ).text()  ==   ' 1 ' ) {
                     
                        tempccno 
=  $(xml).find( " xxNode " ).text(); 
                        $(
" #ccno " ).val(tempccno); 
                    } 
else  { 
                        alert($(xml).find(
" Msg " ).text());
                    }
                } 
// end success: function (xml) ------------
              });
            } 
            
else
            {
                $(
" #ccno " ).val( "" );
                $(
" #ccno " ).focus(); 
            }
//  validateCCNumber(ccno)
         }         
         
// operation is over and enable the button
                 $( " #submit " ).attr( ' disabled ' , false ); 
    });
}) 


转载于:https://www.cnblogs.com/neru/archive/2010/12/21/1912295.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值