javascript - ①$("#userName")[0]中的这个【0】表示什么意思呀?

php中文网
发布: 2016-06-06 20:15:58
原创
2213人浏览过

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;它的作用是什么呢?能否省略?
请大神赐教!

回复内容:

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();
同样的

usernameObj = $("#userName")[0];
if(usernameObj.value==''){
    showWarningMsg("<?php echo $L['enter_you_user_name']; ?>");
    return false;
}
登录后复制

可以等效为

usernameObj = $("#userName")[0];
if(! $("#userName").val().length){
    showWarningMsg("<?php echo $L['enter_you_user_name']; ?>");
    return false;
}
登录后复制

结尾的return false用来阻止事件冒泡,但是源码中的onclick="check_form(); return false;"既然又写了一个return false,那么函数里面那个写不写都没用,反正没挂return。
正确的方式是


因为访问者按回车也会触发表单提交,这样不需要点击submit按钮从而绕过了检测。
再说这个方法最好命名为ajaxSubmit之类的名字,而不是checkform,因为里面包含了提交处理。

立即学习Java免费学习笔记(深入)”;

这段代码的整体逻辑是
当点击提交按钮的时候对form数据进行检查,如果检查成功使用ajax提交数据。return false是用来阻止form产生普通的表单提交。

明白意思就好了,代码本身没什么值得学习的。

1.选择这个是返回jquery对象。
2.是取消动作用的。

建议你重新看下犀牛书,毫无基础的样子。

把jq对象,转换成,原生js对象。此时你不能用jq的方法,只能用js的所支持的dom方法操作。你还可以使用$("#userName").get(0)获取原生js对象,但是建议用数组下标的方法,这种方法更快捷。

①$(selector)返回的是jQuery对象,$(selector)[0]是返回原生的DOM对象,即document.getElementById()返回的那种对象。(不明白这里代码为什么一会用jQuery的方式一会用原生DOM...)
②这个函数是submit form用的吗,如果是的话,return false;的作用是取消form的提交

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号