What does this "0" in JavaScript-①$ ("#userName") [0] mean?

Source: Internet
Author: User
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, "passwor D ": 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://admin & License doesn ' t exist window.location = "i2/wizard/wiz_license.php";                  Break                      Case one://admin & notice something window.location = "i2/wizard/wiz_notice.php";                  Break Case://non-admin user & license doesn ' t exist showwarningmsg ("
  ");                  Break Case://non-admin & Trial license expired showwarningmsg ("
  ");                  Break                      Case://upgrade database Fail errormsg = geterrormsg (data); Showwarningmsg ("
  "+ errormsg);                  Break                      Case 99:errormsg = geterrormsg (data);                      Showwarningmsg (ERRORMSG);              Break     }        }    }); return false;}

What does this "0" in ①$ ("#userName") [0] mean?
The ② function finally adds a return false; what does it do? Can you omit it?
Please enlighten the great God!

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, "passwor D ": 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://admin & License doesn ' t exist window.location = "i2/wizard/wiz_license.php";                  Break                      Case one://admin & notice something window.location = "i2/wizard/wiz_notice.php";                  Break Case://non-admin user & license doesn ' t exist showwarningmsg ("
  ");                  Break Case://non-admin & Trial license expired showwarningmsg ("
  ");                  Break                      Case://upgrade database Fail errormsg = geterrormsg (data); Showwarningmsg ("
  "+ errormsg);                  Break                      Case 99:errormsg = geterrormsg (data);                      Showwarningmsg (ERRORMSG);              Break     }        }    }); return false;}

What does this "0" in ①$ ("#userName") [0] mean?
The ② function finally adds a return false; what does it do? Can you omit it?
Please enlighten the great God!

I am happy to say, rookie look at the rookie write code?
Both sentences are nonsense.
$ (' #userName ') means that the object is found based on the ID, but the ID in the HTML specification is unique, so [0] here is the JS native object, but in fact there is no use, refer to the following
var Yanzheng = $ ("#user_varify"). Val ();
The same

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

can be equivalent to

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

The end of the return false is used to prevent the event bubbling, but the source of the onclick= "Check_form (); return false; " Since write a return false, then the function inside that write not write is useless, anyway did not hang return.
The right way is


because the visitor presses the return will also touch the delivery order submission, so do not need to click the Submit button to bypass the detection.
This method is best named Ajaxsubmit, not Checkform, because it contains commit processing. The overall logic of

This code is
to check the form data when the Submit button is clicked, if the check succeeds in using AJAX to submit data. Return false is used to prevent a form from producing a normal form submission.

It's good to know the meaning, the code itself is nothing to learn.

1. Select this to return the jquery object.
2. The action is canceled. The

Recommends that you look at the Rhino book again, without a basis.

Converts the JQ object into a native JS object. At this point you can not use the JQ method, can only be used by JS supported Dom method operation. You can also use $ ("#userName"). Get (0) Gets the native JS object, but it is recommended to use the array subscript method, which is faster.

$ (selector) returns a JQuery object, $ ( selector) [0] is the object that returns the native DOM object, which is the type returned by document.getElementById () . (I do not understand why the code here is a jquery in the way one will use native dom ...)
② This function is used by the submit form, and if so, return false; The function is to cancel the submission of the form

  • 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.