javascript - ①$("#userName")[0]中的这个【0】表示什么意思呀?
程序员文章站
2024-01-22 21:05:52
...
function check_form(){
usernameObj = $("#userName")[0];
//window.alert(usernameObj.value); //show usernameObj=[object HTMLInputElement]
var language = $("#choose_language").val();
if(usernameObj.value==''){
showWarningMsg("");
return false;
}
pwdObj = $("#password")[0];
if(pwdObj.value==''){
showWarningMsg("");
return false;
}
var yanzheng = $("#user_varify").val();
if(yanzheng==""&&yanzheng.length!=4){
showWarningMsg("");
return false;
}
requestUrl = formatAjaxUrl("std-index.php");
$.post(requestUrl, {"language":language,"action":"login_in","username":usernameObj.value,
"password": hex_md5(pwdObj.value)+"",
"choose": document.getElementById("checkednames").value,
"remember": "0","yanzheng":yanzheng},
function(data, textStatus)
{
if (textStatus=="success") {
errorCode = getErrorCode(data);
switch (errorCode) {
case 0:
window.location = "";
break;
case 1:
case 2:
showWarningMsg("");
break;
case 3:
showWarningMsg("");
break;
case 4:
userHasLogin("");
break;
case 5:
showWarningMsg("");
refresh_img();
break;
case 6:
showWarningMsg("");
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("");
break;
case 13: // non-admin & trial license expired
showWarningMsg("");
break;
case 20: // upgrade database fail
errorMsg = getErrorMsg(data);
showWarningMsg("" + errorMsg);
break;
case 99:
errorMsg = getErrorMsg(data);
showWarningMsg( errorMsg );
break;
}
}
});
return false;
}
①$("#userName")[0]中的这个【0】表示什么意思呀?
②函数最后加了一个return false;它的作用是什么呢?能否省略?
请大神赐教!
回复内容:
function check_form(){
usernameObj = $("#userName")[0];
//window.alert(usernameObj.value); //show usernameObj=[object HTMLInputElement]
var language = $("#choose_language").val();
if(usernameObj.value==''){
showWarningMsg("");
return false;
}
pwdObj = $("#password")[0];
if(pwdObj.value==''){
showWarningMsg("");
return false;
}
var yanzheng = $("#user_varify").val();
if(yanzheng==""&&yanzheng.length!=4){
showWarningMsg("");
return false;
}
requestUrl = formatAjaxUrl("std-index.php");
$.post(requestUrl, {"language":language,"action":"login_in","username":usernameObj.value,
"password": hex_md5(pwdObj.value)+"",
"choose": document.getElementById("checkednames").value,
"remember": "0","yanzheng":yanzheng},
function(data, textStatus)
{
if (textStatus=="success") {
errorCode = getErrorCode(data);
switch (errorCode) {
case 0:
window.location = "";
break;
case 1:
case 2:
showWarningMsg("");
break;
case 3:
showWarningMsg("");
break;
case 4:
userHasLogin("");
break;
case 5:
showWarningMsg("");
refresh_img();
break;
case 6:
showWarningMsg("");
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("");
break;
case 13: // non-admin & trial license expired
showWarningMsg("");
break;
case 20: // upgrade database fail
errorMsg = getErrorMsg(data);
showWarningMsg("" + 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();
同样的
usernameObj = $("#userName")[0];
if(usernameObj.value==''){
showWarningMsg("");
return false;
}
可以等效为
usernameObj = $("#userName")[0];
if(! $("#userName").val().length){
showWarningMsg("");
return false;
}
结尾的return false用来阻止事件冒泡,但是源码中的onclick="check_form(); return false;"既然又写了一个return false,那么函数里面那个写不写都没用,反正没挂return。
正确的方式是