In javascript-① $ ("# userName") [0], what does this [0] mean?

Source: Internet
Author: User
{Code...} ① $ (& quot; # userName & quot;) [0] What does this [0] mean? ② The function adds a returnfalse; what is its function? Can it be omitted? Please enlighten me!
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] What does this [0] mean?
② The function adds a return false; what is its function? Can it be omitted?
Please enlighten me!

Reply content:
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] What does this [0] mean?
② The function adds a return false; what is its function? Can it be omitted?
Please enlighten me!

I can say with pleasure, do cainiao look at the code written by cainiao?
Both statements are nonsense.
$ ('# Username') indicates that the object is searched by id, but the id in the html specification is unique. Therefore, although [0] Here obtains the js native object, but it is not used at all. refer to
Var yanzheng = $ ("# user_varify"). val ();
The same

usernameObj = $("#userName")[0];if(usernameObj.value==''){    showWarningMsg("
  ");    return false;}

It can be equivalent

usernameObj = $("#userName")[0];if(! $("#userName").val().length){    showWarningMsg("
  ");    return false;}

The return false at the end is used to prevent event bubbling, But The onclick = "check_form (); return false;" since another return false statement is written in the source code, it is useless to write the function without writing it, no return is attached.
The correct method is


Because a visitor presses the carriage return and triggers form submission, the detection is bypassed without clicking the submit button.
Besides, we recommend that you name this method ajaxSubmit instead of checkform, because it contains the submission process.

The overall logic of this Code is:
When you click the submit button, check the form data. If the check succeeds, use ajax to submit the data. Return false is used to prevent form from generating normal form submissions.

Just understand the meaning, and the Code itself is nothing worth learning.

1. Select this option to return the jquery object.
2. It is used to cancel the action.

We recommend that you read the rhino book again without any foundation.

Converts a jq object to a native js object. In this case, you cannot use the jq method. You can only use the dom method supported by js. You can also use $ ("# userName"). get (0) to obtain the native js object. However, we recommend that you use the method of array downloading, which is faster.

①$(selector)The returned result is a jQuery object,$(selector)[0]Is to return the native DOM object, that isdocument.getElementById()Returned object. (I don't understand why the code here uses jQuery for a while and uses native DOM ...)
② Is this function used by submit form? If so,return false;Is used to cancel form submission.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.