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
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
});
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
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!