演示见: http://runjs.cn/detail/igwe9bbo 实现把HTML页面的值传递到JavaScript方法中 1. [代码] 将html页面的数据传到Javascript的方法中 htmlmeta http-equiv="Content-Type" content="text/html; charset=utf-8" /headsty
http://runjs.cn/detail/igwe9bbo
实现把HTML页面的值传递到JavaScript方法中
1. [代码]将html页面的数据传到Javascript的方法中
<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <style> h1{ font-family:隶书; color:blue; background-color:orange; text-align:center; } </style> <script type="text/javascript"> function checkfluits(n){ if((n >100) || (n<0)){ return false; }else{ return true; } } function check(){ for (i=0;i<document.forms[0].length-2;i++){ if (document.forms[0].elements[i].value.length==0){ alert(document.forms[0].elements[i].name + "为空!"); return false; } } if (document.forms[0].elements[0].value.length != 3){ alert("用户名必须是3个字符"); return false; } for( i=1;i<4;i++) if (checkfluits(document.forms[0].elements[i].value)==false){ alert(document.forms[0].elements[i].name + "输入量不可用!\n必须在0-100kg的范围内。"); return false; } s="Your input : "; for(i=0;i<document.forms[0].length-2;i++){ box= document.forms[0].elements[i]; s += "\n" + box.name + ": " + box.value; } total = 6.00*document.forms[0].香蕉.value; total += 9.00*document.forms[0].苹果.value; total += 8.00*document.forms[0].鸭梨.value; s += "\n销售额(元):" + total; document.forms[0].output.value = s; return true; } </script> </head> <body> <h1>农夫果园</h1> <form onSubmit="check();return false" name='example'> <table> <tr> <td>用户名</td><td><input type="text" name="用户名" size="30"></td></tr> <tr><td>香蕉(6.00/kg): </td><td><input type="text" name="香蕉" size="30"></tr> <tr><td>苹果(9.00/kg): </td><td><input type="text" name="苹果" size="30"></tr> <tr><td>鸭梨(8.00/kg): </td><td><input type="text" name="鸭梨" size="30"></tr> <tr><td> <input type="submit" value="提交"></td> <td><textarea cols="40" rows="7" name="output">Output Area</textarea></td></tr> </table> </form> </body> </html>