尝试将字符串数组设置回文本区域 [英] trying to set string array back to a text area

查看:92
本文介绍了尝试将字符串数组设置回文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码创建一个数组,然后将其设置为文本区域。

然后我返回它将它拆分回一个数组排序并尝试用新行替换逗号并将其返回到另一个文本区域。

但是我不能替换逗号或用值设置第二个文本区域。

有没有人有任何想法?我真的很感激。

The code creates an array then sets it to a text-area.
I then return it turn split it back to an array sort it and attempt to replace the commas with new lines and return it to another text-area.
However I can''t replace the commas or set the second text-area with the values.
Does anyone have any ideas? I really appreciate it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <title>Grocery List</title>
    <script type="text/javascript">
   
        
		function addItem() {

            var myForm = document.forms.groceryList;
            var listAdd = [];
            for (i = 0; i < myForm.category.length; i++) {

                if (myForm.category[i].checked) {

                    break;

                }

            }

            type = myForm.category[i].value;

     

            var myItem = document.getElementById("itemName").value;

           

        	listAdd.push(type + " : "  + myItem + "\n");

            

			var area = document.getElementById('list');

			//add array values to text area as a string

			area.value = listAdd + area.value;

			



}



function printSortedList()

{

	var myForms = document.forms.groceryList;

	newList = document.getElementById('list')

	newValue = newList.value;

	//pattern = /\n/g,',';

	//newSort = newSort.replace(/\n/g,',');

   //alert(newValue);

   newSort = newValue.split('\n');

   

   //alert(newSort);

   alphaValue=newSort.sort();

   alert(alphaValue);

   //alphaList = new Array;

   alphaList = alphaValue.value;

	alphaSort = alphaList.replace(/,/g, "\n");

   alert(alphaSort);

   //pattern = /,/g,'\n';

   //for ( i = 0; i < newSort.length; i++) {

 

   

   //alhpaList[i] = alphaSort[i].replace (pattern)

   //}

	//x = myForm.list.length;

	//alert(x);

	

	

	//newAlpha = newSort.sort();

	//alert(alphaList);

	//newAlpha.toString();

	//newValue = newAlpha.replace(/,/g,'\n');

	//listAlpha = newValue.split(',');

   // alert(alphaSort);

	/*alphaAdd = newArray;



	for (j=o;i<myforms.list.length;j++)>
	{
		alphaAdd.push(alphaSort[j] + "\n");
	}
	alert(alphaAdd);
	*/
	var tArea = document.getElementById('sorted');
	
	tArea.value = alphaSort;
	//document.getElementById('list').value = alphaSort;
}
</script>
<style type="text/css">
  body,td,th {
	color: #00F;
}
body {
	background-color: #E4E4E4;
	background-image: url(grocery_bag.jpg);
}
</style>
  </head>
  <body>
  <form action="#" name="groceryList">
	
  Category: <input type="radio" name="category" value="Produce" />Produce
  <input type="radio" name="category" value="Meat and Cheese" />Meat and Cheese
  <input type="radio" name="category" value="Grocery" />Grocery
  <input type="radio" name="category" value="Beverage" />Beverage
  
  <br />
  
  Item: <input name="item" type="text" id="itemName" size="30" />
  <br />
  <input type="button" name="add" value="Add to List"  önclick="addItem()"/>
  <br />
 <textarea name="list" rows="30" id="list" style="width:500" size="15"></textarea>

   
 <textarea name="sorted" rows="30" id="sorted" style="width:500" size="15"></textarea>
  
<br />  
    
  <input type="button" id="button" name="sort" value="Print Sorted List"

 önclick="printSortedList()"/>
  </form>
  </body>
</html>

推荐答案

The first thing that stands out is that sort() sorts the members of the original array so you don’’t need to assign the result to another array.



Converting an array to a string performs a join with a comma as the separator - the easiest way to use a \"\n\" instead of a comma is to explicitly use theArray.join(’’\n’’).
The first thing that stands out is that sort() sorts the members of the original array so you don''t need to assign the result to another array.

Converting an array to a string performs a join with a comma as the separator - the easiest way to use a "\n" instead of a comma is to explicitly use theArray.join(''\n'').


这篇关于尝试将字符串数组设置回文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆