[英]How to get specific data from input/textarea
I need some help as a beginner in figuring way to pull specific data that was input via textarea.作为初学者,我需要一些帮助来找出通过 textarea 输入的特定数据的方法。
Lets say I have textarea that user uses for input of certain data, which simply stores input in a variable.假设我有用户用于输入某些数据的 textarea,它只是将输入存储在一个变量中。 Problem I have is how to pull certain parts of that data (stored in variable now) when there is no set pattern on how data is entered.
我的问题是当没有关于如何输入数据的固定模式时,如何提取该数据的某些部分(现在存储在变量中)。
For example this could be something user would enter:例如,这可能是用户会输入的内容:
Name: Random Name名称:随机名称
Age: 20年龄: 20
Random Number: 19-12随机数: 19-12
Phone: 111-111-222电话: 111-111-222
Now, I understand I could use different methods to fetch data when I know its location in variable or when enter pattern is always same but what if it's different.现在,我明白我可以使用不同的方法来获取数据,当我知道它在变量中的位置或者当输入模式总是相同但如果它不同时怎么办。 What if someone writes single digit in Age, Phone format is different or random number is different.
如果有人在 Age、Phone 格式不同或随机数不同的情况下写个位数怎么办。
Hopefully I was clear enough with my problem.希望我对我的问题足够清楚。 Thanks in advance for your help!
在此先感谢您的帮助!
If you are pretty sure that user would always make use of a colon (:) after the field, use this code:如果您非常确定用户总是在字段后使用冒号 (:),请使用以下代码:
var data = document.querySelector('textarea').value;
var data2 = data.split('\n');
var name = data2[0].split(':')[1];
var age = data2[1].split(':')[1];
var number = data2[2].split(':')[1];
var phone = data2[3].split(':')[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.