有1个文本框输入数值(1~100)点击按钮生成2个随机数相加等于文本框的值用JS怎么写

解决方案 »

  1.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>Untitled Document</title>
    </head>
    <body>
    请输入一个数字:<input type="text" id="total" name="total"><br>
    <input type="button" id="btn" name="btn" value="产生随机算式" onclick="getRandom();"><br>

    <input type="text" id="arg1" name="arg1">
    +
    <input type="text" id="arg2" name="arg2">
    </body>
    <script language="JavaScript">
    <!--
    function getRandom(){
    var total = document.getElementById("total").value;
    if(!total || total.replace(/^\s*|\s*$/g,'').length==0){
    alert("请选输入总数!");
    return;
    }
    total = total.replace(/^\s*|\s*$/g,'');
    if(total.replace(/\d+/g,'').length>0){
    alert("您输入的不是一个数字!");
    return;
    }
    total =total *1;
    var arg1 =  Math.round(Math.random()*total);
    var arg2 = total - arg1;
    document.getElementById("arg1").value=arg1;
    document.getElementById("arg2").value=arg2;
    }
    //-->
    </script>
    </html>随机数的产生 可以根据你输入的那个数,限制范围
      

  2.   

    在这里学到了获取随机数的函数“Math.random()”好!