之前用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");
于是删掉了一点细节,其大致的代码如下~
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
$(
'
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 );
});
})
{
// //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 );
});
})