JavaScript [Object,object]

Source: Internet
Author: User

When assigning an object to an HTML tag's properties today, it is a bit strange to find that the value taken is [Object,object].

The code is as follows:

1 <!DOCTYPE HTML>2 <HTML>3     <Head>4         <MetaCharSet= "UTF-8">5         <title>[Object Object]</title>6         <Scripttype= "Text/javascript"src=".. /.. /trd/jquery/jquery-1.9.1.min.js "></Script>7         <Scripttype= "Text/javascript">8             $(function(){9                 varStudent={name:"Xiao Ming", Gender:"male"};Ten                 $("#txt"). attr ("RowData", student);//Add the RowData property to the label with the ID txt and the value to the student object One                  A Console.log ($ ("#txt"). attr ("RowData"));//console output is: [Object Object] - Alert ($ ("#txt"). attr ("RowData"));//The information in the popup box is: [Object Object] -             }); the         </Script>         -     </Head> -     <Body> -         <inputtype= "text"name= "txt"ID= "txt"/> +     </Body> - </HTML>

So how do you get the value of an object? My practice is to assign values by first converting the student object to a JSON string using the Json.stringify method.

The code is as follows:

1 <!DOCTYPE HTML>2 <HTML>3     <Head>4         <MetaCharSet= "UTF-8">5         <title>[Object Object]</title>6         <Scripttype= "Text/javascript"src=".. /.. /trd/jquery/jquery-1.9.1.min.js "></Script>7         <Scripttype= "Text/javascript">8             $(function(){9                 varStudent={name:"Xiao Ming", Gender:"male"};Ten                 $("#txt"). attr ("RowData", json.stringify (student));//Add the RowData property to the label with the ID txt and the value to the student object One                  A Console.log ($ ("#txt"). attr ("RowData"));//Console output is: {"name": "Xiaoming", "gender": "Male"} - Alert ($ ("#txt"). attr ("RowData"));//The information in the popup box is: {"name": "Xiaoming", "gender": "Male"} -             }); the         </Script>         -     </Head> -     <Body> -         <inputtype= "text"name= "txt"ID= "txt"/> +     </Body> - </HTML>

But I have another question is: Why the alert (object) output value is [Object,object], if you see this article Bo friends know the answer, but also hope to enlighten.

JavaScript [Object,object]

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.