最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to parse the data given in input text using JavaScript? - Stack Overflow

programmeradmin1浏览0评论

I have to paste the data in HTML input text fields in the following format from MS excel or same type of sheets:

123456  12  this
234567  3   before blank
111111  3   
222222  3   after blank

I copied the data from MS Excel which contain the single tab between 12 and this and so on.. I have tried to implement it but didn't work for me. The above data will be pasted into a text area and it save into a another JavaScript variable into the following format:

<row dc= '" + arr[0] + "' al='" + arr[1] + "' msg='" + arr[2] + "' />

Where arr[0] is 123456, and arr[1] is 12 and arr[3] is this and so on by appending the value.

I have to paste the data in HTML input text fields in the following format from MS excel or same type of sheets:

123456  12  this
234567  3   before blank
111111  3   
222222  3   after blank

I copied the data from MS Excel which contain the single tab between 12 and this and so on.. I have tried to implement it but didn't work for me. The above data will be pasted into a text area and it save into a another JavaScript variable into the following format:

<row dc= '" + arr[0] + "' al='" + arr[1] + "' msg='" + arr[2] + "' />

Where arr[0] is 123456, and arr[1] is 12 and arr[3] is this and so on by appending the value.

Share Improve this question edited Aug 29, 2022 at 22:09 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 3, 2012 at 18:31 Amit PalAmit Pal 11.1k31 gold badges85 silver badges169 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3
var str=document.getElementById("myTextarea").value
var t=str.split("\n");
for (var i=0;i<t.length;i++)
{
  var arr=t[i].split("\t");
  document.write("'<row dc= '" + arr[0] + "' al='" + arr[1] + "' msg='" + arr[2] + "' />'")
}

Working Sample:-

<!DOCTYPE html>
<html>
<body>
<script>
function submit_a () {
var str=document.getElementById("allocationstatus").value
//str=str.replace('\t','&#09;');
//document.getElementById("allocationstatus").value=str;
var t=str.split("\n");
var s='';
for (var i=0;i<t.length;i++)
{
  var arr=t[i].split("\t");
  s ="'<row dc= '" + arr[0] + "' al='" + arr[1] + "' msg='" + arr[2] + "' />'";
  alert(arr.length);
  for (var j=0;j<arr.length;j++)
{
  //alert(arr[j]);
}
  //document.write(s);
}
}
</script>
<textarea rows = "5" cols = "80"  id = "allocationstatus" >stat&#09;do&#09;one
stat1&#09;do1&#09;one1
stat2&#09;do1&#09;one1</textarea>
<input type = "button" onclick = "submit_a();"/>

</body>
</html> 
发布评论

评论列表(0)

  1. 暂无评论