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

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

Jan 22, 2024 pm 12:48 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Effortles Fixes for Black Screen After Installing a Graphics Driver Effortles Fixes for Black Screen After Installing a Graphics Driver Apr 15, 2025 am 12:11 AM

Have you ever encountered a black screen after installing a graphics driver like an Nvidia driver in Windows 10/11? Now in this post from php.cn, you can find a couple of worth trying solutions to the Nvidia driver update black screen.

KB2267602 Fails to Install: Here Is How to Fix It! KB2267602 Fails to Install: Here Is How to Fix It! Apr 15, 2025 am 12:48 AM

KB2267602 is a protection or definition update for Windows Defender designed to fix vulnerabilities and threats in Windows. Some users reported that they were unable to install KB2267602. This post from php.cn introduces how to fix the “KB2267602 fai

Difference Between RAID Recovery and Hard Drive Recovery Difference Between RAID Recovery and Hard Drive Recovery Apr 17, 2025 am 12:50 AM

Data recovery is always a heated topic. To successfully restore data from your device, you should know how it stores data. You can learn the difference between RAID recovery and hard drive recovery from this php.cn post.

How to Fix the File System Error (-1073741521) in Windows? - MiniTool How to Fix the File System Error (-1073741521) in Windows? - MiniTool Apr 16, 2025 am 12:37 AM

File system errors commonly happen on people’s computer and the error can trigger a series of linked malfunctions. This article on php.cn Website will give you a series of fixes to targeting the file system error (-1073741521). Please keep on with yo

Fix Security Tab Not showing in Folder Properties Windows 11 Fix Security Tab Not showing in Folder Properties Windows 11 Apr 17, 2025 am 12:36 AM

The Security tab in File Properties helps set different permissions for different groups and users to a file or folder. Some users find that Windows 11 Security tab missing from File Properties. This post from php.cn gives some methods to fix it.

Community Tips for Black Folder Background Windows 10/11 Community Tips for Black Folder Background Windows 10/11 Apr 15, 2025 am 12:40 AM

Seeing a black folder background Windows 10/11 when you open File Explorer? In this post from php.cn Solution, you will learn a couple of useful solutions to remove the black background in folders.

The File Can't Be Displayed in OneDrive - How to Resolve It? The File Can't Be Displayed in OneDrive - How to Resolve It? Apr 18, 2025 am 12:47 AM

Are you struggling with the “the file can’t be displayed” error when accessing the specific folder? Some users are complaining about this trouble and looking for useful measures. This article about the file can’t be displayed OneDrive from php.cn wil

Guide - How to Stop Expired Windows Server Auto-Shutdown? Guide - How to Stop Expired Windows Server Auto-Shutdown? Apr 18, 2025 am 12:48 AM

Some users report that they meet the Windows Server auto-shutdown issue after the license expires. This post from php.cn teaches you how to stop expired Windows Server auto-shutdown. Now, keep on your reading.

See all articles