要求:在输入框里只能输入01 25 31 10 02 06 这样的用空格分割的两位数字
按排序按钮后 在下面的输入框排列成:01 02 06 10 25 31 按数字大小排序的结果。
要整个页面里的源代码,复制后就能见到效果的
类似这样的效果:<!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>
</head>
<body>
<form>
<table border=1>
<tr>
<td>输入要排序的代码: <input type=text name=numbers size=75 value=""></td>
</tr>
<tr>
<td>排序结果: <input type=text name=answers size=75></td>
</tr>
<tr>
<td colspan=2 align=center><input type=button value="排序" onClick="doSort(this.form)">
</td>
</tr>
</table>
</form>
</body>排序

解决方案 »

  1.   

    <!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>
        <script type="text/javascript">
        function doSort(){
            document.getElementById('answers').value = document.getElementById('numbers').value.split(' ').sort().join(' ');
        }
        </script>

    </head>
    <body>
    <form>
    <table border=1>
    <tr>
    <td>输入要排序的代码: <input type="text" id="numbers" name="numbers" size="75" value=""></td>
    </tr>
    <tr>
    <td>排序结果: <input type="text" id="answers" name="answers" size="75"></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><input type="button" value="排序" onClick="doSort()">
    </td>
    </tr>
    </table>
    </form>
    </body>
      

  2.   


    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
    $(function(){
    $("#abv").click(function(){
    var a=$("#numbers").val().split(' ').sort().join(' ');
    $("#answers").val(a);
    });
    })
     </script>
     <table border=1>
     <tr>
     <td>输入要排序的代码: <input type="text" id="numbers" size=75 ></td>
     </tr>
     <tr>
     <td>排序结果: <input type="text" id="answers" size=75></td>
     </tr>
     <tr>
     <td colspan=2 align="center"><input type="button" id="abv" value="排序"/>
     </td>
     </tr>
     </table>
      

  3.   

    var nums= [1, 3, 5, 2];
    nums.sort();
      

  4.   


    var numbers = $('input[name="numbers"]').val().split(" ");
    numbers.sort();
    $('input[name="answers"]').val(numbers.join(" "));
      

  5.   

    <!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>
    </head>
    <body>
    <script>
    function check(e,o){
      var kc=e.keyCode,len=o.value.length,mod=len%3;
      if(kc==8)return
      if((mod<3&&(kc<48||kc>57))){
        if(e.preventDefault){e.preventDefault();} 
        e.returnValue=false
      }else if(mod==2)o.value+=' ';
    }
    function doSort(){
      var s=document.getElementById('s'),t=document.getElementById('t')
      document.getElementById('t').value=document.getElementById('s').value.split(' ').sort().join(' ')
    }
    </script>
    <form>
    <table border=1>
    <tr>
    <td>输入要排序的代码: <input type=text name=numbers size=75 value="" onkeydown="check(event,this)" id="s"></td>
    </tr>
    <tr>
    <td>排序结果: <input type=text name=answers size=75 id="t"></td>
    </tr>
    <tr>
    <td colspan=2 align=center><input type=button value="排序" onClick="doSort(this.form)">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>