Using JavaScript how to extract textbox content through loop and convert it to JSON format to display in new window

PHPz
Release: 2024-01-22 12:48:12
forward
404 people have browsed it

Using JavaScript how to extract textbox content through loop and convert it to JSON format to display in new window

How to extract the text box content in a loop in javascript and make it into json format and send it to a new window for display

Your ids are not consecutive, this id attribute is useless

I think of two methods

1,

You can take out all the text boxes on the page and traverse the dom or use jquery $("texterea") or querySelectorAll("textarea")

var data={},nodes=querySelectorAll("textarea");//All text boxes may be useful

for(var i=0,l=nodes.length;i

data[nodes[i].id]=nodes[i].value; //If there are text boxes that do not meet the conditions, you can also filter them out

};

var json=JSON.stringify(data);//Json comes out and is sent to a new page? I don’t know what you want 2.

When your program outputs the page, it should also output json. The prerequisite is that the page is written by you

How to get data from json

Expand All

According to the returned string, it can be seen that it is in the form of a js array spliced ​​into multiple jsons.

If only a string is returned, the string must be converted into a js object.

Use Jquery's $.each() method to loop through the js array to retrieve the data of each json object.

str = '[{"key":"value","keys":[{"key1":"value1","key2":"value2"},{"key1":"value3","key2 ":"value4"}],"obj":{"id":1,"msg":"success"}}]';

str_json = eval("(" str ")"); //Convert string into js object

$.each(str_json,fucntion(a,b){

alert(a); //Pop up the key of the array

alert(b.id);//Pop up the data to be taken out

});

How does JS javascript convert an ordinary array into a JSON array

First of all, there is a syntax error in your array~

In JS, json is a string representation. Your B is obviously an object and has nothing to do with json~

So it is converting between js objects (arrays) and json strings~

Then your A (the example in js uses Camel rules, so it should be written as a lowercase a) should be written as:

var a = {

name: "XXXX", //Attributes are also Camel rules

year: 1990,

old: 21

};

Then there is the conversion problem, using the JSON class:

var b = JSON.stringify(a); //Note that b is a json string

If the JSON object cannot be found, your browser version is too old

You need to manually reference the json class library

Please download and quote "json2.js" by yourself

Hope it helps you~

By Billskate

How does JS write json format data into an array

json array zhidao is also an array

//1,

var jsonstr="[{'name':'a','value':1},{'name':'b','value':2}]";

var jsonarray = eval('(' jsonstr ')');

var arr =

{

"name" : $('#names').val(),

"value" : $('#values').val()

}

jsonarray.push(arr);

//2,

var json={};//Define a json object

json.array1=["2","4"];//Add a new attribute, this attribute is an array

json.array1[json.array1.length]='6'; //Append an element to the array

alert(json.array1)

The above is the detailed content of Using JavaScript how to extract textbox content through loop and convert it to JSON format to display in new window. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!