function check_form(){
usernameObj = $("#userName")[0];
//window.alert(usernameObj.value); //show usernameObj=[object HTMLInputElement]
var language = $("#choose_language").val();
if(usernameObj.value==''){
showWarningMsg("<?php echo $L['enter_you_user_name']; ?>");
return false;
}
pwdObj = $("#password")[0];
if(pwdObj.value==''){
showWarningMsg("<?php echo $L['enter_you_user_password']; ?>");
return false;
}
var yanzheng = $("#user_varify").val();
if(yanzheng==""&&yanzheng.length!=4){
showWarningMsg("<?php echo $L['enter_you_user_yz']; ?>");
return false;
}
requestUrl = formatAjaxUrl("std-index.php");
$.post(requestUrl, {"language":language,"action":"login_in","username":usernameObj.value,
"password": hex_md5(pwdObj.value)+"<?php echo $_SESSION["salt"]; ?>",
"choose": document.getElementById("checkednames").value,
"remember": "0","yanzheng":yanzheng},
function(data, textStatus)
{
if (textStatus=="success") {
errorCode = getErrorCode(data);
switch (errorCode) {
case 0:
window.location = "<?php echo HOME_URL; ?>";
break;
case 1:
case 2:
showWarningMsg("<?php echo $L['user_password_not_match']; ?>");
break;
case 3:
showWarningMsg("<?php echo $L['MSG_ERROR_LOCK']; ?>");
break;
case 4:
userHasLogin("<?php echo $L['MSG_ERROR_RELOGIN']; ?>");
break;
case 5:
showWarningMsg("<?php echo $L['user_yz_not_match']; ?>");
refresh_img();
break;
case 6:
showWarningMsg("<?php echo $L['user_yz_outtime_login_agin']; ?>");
refresh_img();
break;
case 9:
break;
case 10: // admin & license doesn't exist
window.location = "i2/wizard/wiz_license.php";
break;
case 11: // admin & notice something
window.location = "i2/wizard/wiz_notice.php";
break;
case 12: // non-admin user & license doesn't exist
showWarningMsg("<?php echo $L['SW_LICENSE_UNREGISTER']; ?>");
break;
case 13: // non-admin & trial license expired
showWarningMsg("<?php echo $L['SW_LICENSE_EXPIRED']; ?>");
break;
case 20: // upgrade database fail
errorMsg = getErrorMsg(data);
showWarningMsg("<?php echo $L['MSG_ERROR_DB_CHECK_FAIL']; ?>" + errorMsg);
break;
case 99:
errorMsg = getErrorMsg(data);
showWarningMsg( errorMsg );
break;
}
}
});
return false;
}
①$("#userName")[0]中的这个【0】表示什么意思呀?
②函数最后加了一个return false;它的作用是什么呢?能否省略?
请大神赐教!
我能很喜感的说,菜鸟看菜鸟写的代码吗?
两句都是废话。
$('#userName')表示根据id查找对象,但是html规范中id是唯一的,所以这里的[0]虽然是得到了js原生对象,但是实际上一点用都没有,参考后面的
var yanzheng = $("#user_varify").val();
同样的
可以等效为
结尾的return false用来阻止事件冒泡,但是源码中的onclick="check_form(); return false;"既然又写了一个return false,那么函数里面那个写不写都没用,反正没挂return。
正确的方式是
<form onsubmit="return checkform()">
因为访问者按回车也会触发表单提交,这样不需要点击submit按钮从而绕过了检测。
再说这个方法最好命名为ajaxSubmit之类的名字,而不是checkform,因为里面包含了提交处理。
这段代码的整体逻辑是
当点击提交按钮的时候对form数据进行检查,如果检查成功使用ajax提交数据。return false是用来阻止form产生普通的表单提交。
明白意思就好了,代码本身没什么值得学习的。