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

WBOY
Lepaskan: 2016-06-06 20:15:58
asal
2020 orang telah melayarinya

<code>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;
}</code>
Salin selepas log masuk
Salin selepas log masuk

①$("#userName")[0]中的这个【0】表示什么意思呀?
②函数最后加了一个return false;它的作用是什么呢?能否省略?
请大神赐教!

回复内容:

<code>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;
}</code>
Salin selepas log masuk
Salin selepas log masuk

①$("#userName")[0]中的这个【0】表示什么意思呀?
②函数最后加了一个return false;它的作用是什么呢?能否省略?
请大神赐教!

我能很喜感的说,菜鸟看菜鸟写的代码吗?
两句都是废话。
$('#userName')表示根据id查找对象,但是html规范中id是唯一的,所以这里的[0]虽然是得到了js原生对象,但是实际上一点用都没有,参考后面的
var yanzheng = $("#user_varify").val();
同样的

<code>usernameObj = $("#userName")[0];
if(usernameObj.value==''){
    showWarningMsg("<?php echo $L['enter_you_user_name']; ?>");
    return false;
}</code>
Salin selepas log masuk

可以等效为

<code>usernameObj = $("#userName")[0];
if(! $("#userName").val().length){
    showWarningMsg("<?php echo $L['enter_you_user_name']; ?>");
    return false;
}</code>
Salin selepas log masuk

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


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

这段代码的整体逻辑是
当点击提交按钮的时候对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的提交

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!