用javascript来比较四个数大小
2个回答
展开全部
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/jscript" language="javascript">
function order(){
if(document.getElementById("txtNumA").value==""||document.getElementById("txtNumB").value==""
||document.getElementById("txtNumC").value==""||document.getElementById("txtNumD").value==""){
alert("请输入数字!");
}else{
var num=new Array();
var temp;
var numA=parseFloat(document.getElementById("txtNumA").value);
var numB=parseFloat(document.getElementById("txtNumB").value);
var numC=parseFloat(document.getElementById("txtNumC").value);
var numD=parseFloat(document.getElementById("txtNumD").value);
num[0]=numA;
num[1]=numB;
num[2]=numC;
num[3]=numD;
for(var i=0;i<num.length;i++){
for(var j=0;j<num.length-1-i;j++){
if(num[j]>num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
}
for(var i=0;i<num.length;i++){
if(i==num.length-1){
document.getElementById("txtOrder").value=num.toString();
}else{
document.getElementById("txtOrder").value=(num.toString()+",");
}
}
}
function orderDesc(){
if(document.getElementById("txtNumA").value==""||document.getElementById("txtNumB").value==""
||document.getElementById("txtNumC").value==""||document.getElementById("txtNumD").value==""){
alert("请输入数字!");
}else{
var num=new Array();
var temp;
var numA=parseFloat(document.getElementById("txtNumA").value);
var numB=parseFloat(document.getElementById("txtNumB").value);
var numC=parseFloat(document.getElementById("txtNumC").value);
var numD=parseFloat(document.getElementById("txtNumD").value);
num[0]=numA;
num[1]=numB;
num[2]=numC;
num[3]=numD;
for(var i=0;i<num.length;i++){
for(var j=0;j<num.length-1-i;j++){
if(num[j]<num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
}
for(var i=0;i<num.length;i++){
if(i==num.length-1){
document.getElementById("txtOrderDec").value=num.toString();
}else{
document.getElementById("txtOrderDec").value=(num.toString()+",");
}
}
}
function show(){
order();
orderDesc();
}
</script>
<title>我的第一个网页</title>
</head>
<body>
<form id="MyForm" name="MyForm" method="post" action="">
<table width="348" border="0">
<tr>
<td colspan="2" align="center" valign="middle"><strong>数字排序</strong></td>
</tr>
<tr>
<td width="131">第一个数字:</td>
<td width="228"><input type="text" name="txtNumA" id="txtNumA" /></td>
</tr>
<tr>
<td>第二个数字:</td>
<td><input type="text" name="txtNumB" id="txtNumB" /></td>
</tr>
<tr>
<td>第三个数字</td>
<td><input type="text" name="txtNumC" id="txtNumC" /></td>
</tr>
<tr>
<td>第四个数字:</td>
<td><input type="text" name="txtNumD" id="txtNumD" /></td>
</tr>
<tr>
<td>从小到大排序:</td>
<td><input name="txtOrder" type="text" id="txtOrder" readonly="readonly" /></td>
</tr>
<tr>
<td>从大到小排序:</td>
<td><input name="txtOrderDec" type="text" id="txtOrderDec" readonly="readonly" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" name="btnSubmit" id="btnSubmit" value="排 序" onclick="show()"/></td>
</tr>
</table>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/jscript" language="javascript">
function order(){
if(document.getElementById("txtNumA").value==""||document.getElementById("txtNumB").value==""
||document.getElementById("txtNumC").value==""||document.getElementById("txtNumD").value==""){
alert("请输入数字!");
}else{
var num=new Array();
var temp;
var numA=parseFloat(document.getElementById("txtNumA").value);
var numB=parseFloat(document.getElementById("txtNumB").value);
var numC=parseFloat(document.getElementById("txtNumC").value);
var numD=parseFloat(document.getElementById("txtNumD").value);
num[0]=numA;
num[1]=numB;
num[2]=numC;
num[3]=numD;
for(var i=0;i<num.length;i++){
for(var j=0;j<num.length-1-i;j++){
if(num[j]>num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
}
for(var i=0;i<num.length;i++){
if(i==num.length-1){
document.getElementById("txtOrder").value=num.toString();
}else{
document.getElementById("txtOrder").value=(num.toString()+",");
}
}
}
function orderDesc(){
if(document.getElementById("txtNumA").value==""||document.getElementById("txtNumB").value==""
||document.getElementById("txtNumC").value==""||document.getElementById("txtNumD").value==""){
alert("请输入数字!");
}else{
var num=new Array();
var temp;
var numA=parseFloat(document.getElementById("txtNumA").value);
var numB=parseFloat(document.getElementById("txtNumB").value);
var numC=parseFloat(document.getElementById("txtNumC").value);
var numD=parseFloat(document.getElementById("txtNumD").value);
num[0]=numA;
num[1]=numB;
num[2]=numC;
num[3]=numD;
for(var i=0;i<num.length;i++){
for(var j=0;j<num.length-1-i;j++){
if(num[j]<num[j+1]){
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
}
for(var i=0;i<num.length;i++){
if(i==num.length-1){
document.getElementById("txtOrderDec").value=num.toString();
}else{
document.getElementById("txtOrderDec").value=(num.toString()+",");
}
}
}
function show(){
order();
orderDesc();
}
</script>
<title>我的第一个网页</title>
</head>
<body>
<form id="MyForm" name="MyForm" method="post" action="">
<table width="348" border="0">
<tr>
<td colspan="2" align="center" valign="middle"><strong>数字排序</strong></td>
</tr>
<tr>
<td width="131">第一个数字:</td>
<td width="228"><input type="text" name="txtNumA" id="txtNumA" /></td>
</tr>
<tr>
<td>第二个数字:</td>
<td><input type="text" name="txtNumB" id="txtNumB" /></td>
</tr>
<tr>
<td>第三个数字</td>
<td><input type="text" name="txtNumC" id="txtNumC" /></td>
</tr>
<tr>
<td>第四个数字:</td>
<td><input type="text" name="txtNumD" id="txtNumD" /></td>
</tr>
<tr>
<td>从小到大排序:</td>
<td><input name="txtOrder" type="text" id="txtOrder" readonly="readonly" /></td>
</tr>
<tr>
<td>从大到小排序:</td>
<td><input name="txtOrderDec" type="text" id="txtOrderDec" readonly="readonly" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" name="btnSubmit" id="btnSubmit" value="排 序" onclick="show()"/></td>
</tr>
</table>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询