单击“添加到列表”按钮,该项目将添加到列表文本框中

时间:2012-04-25 13:47:25

标签: javascript javascript-events

当用户检查一个类别并键入一个项目并点击添加到列表按钮时 item被添加到列表文本框中,当用户以任何顺序输入项目时,他们可以使用Print Sorted List按钮创建所有添加的杂货的排序列表。感谢

 <title>Grocery List</title>
 <script language="JavaScript">
 <!-- HIDE FROM INCOMPATIBLE BROWSERS
 function addItem() {

 //To add a variable to an object value:
 var x = y + ' > ' + document.MyForm.item.value;
 //To add an option to a listbox:
 addListItem = new Option(x,x);
 document.MyForm.list.options[i++] = addListItem;

 }

 function printSortedList() {
 //To determine which radio button has been selected:
 for (var i=0; i < document.MyForm.category.length; i++)
 {
 if (document.MyForm.category[i].checked)
 {
 var y = MyForm.category[i].value;
 }
 }

 }
 // STOP HIDING FROM INCOMPATIBLE BROWSERS-->
 </script>
 </head>
 <body>
 <form name="groceryList">
 Category: <input type="radio" name="category" value="Produce">Produce</input>
 <input type="radio" name="category" value="Meat and Cheese">Meat and Cheese</input>
 <input type="radio" name="category" value="Grocery">Grocery</input>
 <input type="radio" name="category" value="Beverage">Beverage</input>
 <br />
 Item: <input type="text" length="30" name="item" />
 <p />
 <input type="button" name="add" value="Add to List" onClick="addItem()"/>
 <p />
 <select name="list" size="10" style="width:500"></select>
 <p />
 <input type="button" name="sort" value="Print Sortedlist"onClick="printSortedList()"/>
 </form>
 </body>
 </html>

0 个答案:

没有答案