使用Jquery或PHP导入数据的最佳方法是什么?
我需要读取文本区域,并识别像这样的块
name:这是名字,age:这是年龄我只需要得到名字之间的部分:我需要得到这个年龄:
我不知道怎么做,我需要用什么来做这个。所以我需要知道如何开始?
jquery选择器,不重要,因为我可以使用任何想法,如更改,或按钮点击功能,这是无关紧要的…
<textarea id='import'>
this is pasted text...
name: this is name
age: this is age
other: have but i dont need
other2: have but i dont need
city: this is city
obs: this is obs </textarea>
<input type="text" id="name">
<input type="text" id="age">
<input type="text" id="city">
<input type="text" id="obs">
<button>Import</button>
你可以试试下面的javascript代码:
var input = document.getElementById("import");
input.addEventListener('input', function(){
var content = input.value;
content.replace(/('S+): ?(.+)/g, function(m, id, value) {
var element = document.getElementById(id);
if (element) element.value = value;
});
});
<textarea id="import"></textarea>
<br>name: <input type="text" id="name">
<br>age: <input type="text" id="age">
<br>city: <input type="text" id="city">
<br>obs: <input type="text" id="obs">
您可以在这里看到regex的作用