日期:2014-05-16  浏览次数:20500 次

javascript 如何定义集合
现在页面上有两三个文本框!
我想用   javascript   先把这三个值放到一个集合里
然后再用一个什么函数取最大值和最小值,行吗?
请各位大虾帮帮忙!!

------解决方案--------------------
这样最好用数组,然后用数组的排序 sort(),再取第一个和最后一个值就OK了!
------解决方案--------------------
排序的效率并不是十分的高,你可以使用 Math.max() 此类的函数来完成:

<input id= "t1 " type= "text " value= "3 ">
<input id= "t2 " type= "text " value= "8 ">
<input id= "t3 " type= "text " value= "6 ">
<input type= "button " value= "look " onclick= "look(); ">
<script>
function look()
{
var list = new Array(3);
list[0] = document.getElementById( "t1 ").value;
list[1] = document.getElementById( "t2 ").value;
list[2] = document.getElementById( "t3 ").value;
//list.sort();
//alert( "min: "+list[0]+ ",max: "+list[list.length-1]);
alert(Math.max.apply(null, list));
alert(Math.min.apply(null, list));
}
</script>