how2j.cn


工具版本兼容问题
<form>用于向服务器提交数据,比如账号密码

示例 1 : 表单   
示例 2 : method="get"   
示例 3 : method="post"   
示例 4 : get和post的区别   

示例 1 :

表单

action="/study/login.jsp" 表示把账号和密码提交到login.jsp这个页面去

注: 这里把数据提交到服务端的login.jsp去了,鉴于当前学习的进度,就不对JSP的内容展开了。 JSP的内容可以到JSP章节进行学习
注: 有好奇的同学可以参考图片中的login.jsp
"); window.frames["iframe_show360"].document.write(decodeHtml($("textarea#stepcodeTextarea360").val())); window.frames["iframe_show360"].document.close(); $(window.frames["iframe_show360"]).load(function(){ $("#iframe_show360").height($("#iframe_show360").contents().find("body").height()+showittryitheight); }); $("#iframe_show360").height($("#iframe_show360").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
表单
<form action="http://127.0.0.1/study/login.jsp"> 账号:<input type="text" name="name"> <br/> 密码:<input type="password" name="password" > <br/> <input type="submit" value="登陆"> </form>
<form action="http://127.0.0.1/study/login.jsp">
账号:<input type="text" name="name"> <br/>
密码:<input type="password" name="password" > <br/>
<input type="submit" value="登陆">
</form>
"); window.frames["iframe360"].document.write(decodeHtml(code360)); window.frames["iframe360"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe360"]).load(function(){ $("#iframe360").height($("#iframe360").contents().find("body").height()+showittryitheight); }); $("#iframe360").height($("#iframe360").contents().find("body").height()+showittryitheight); alreadyWriteCode360 = code360; $("#rendering360").hide(); $("#rendered360").show(); } var tRereshRetry2DemoPanel360 = setInterval(rereshRetry2DemoPanel360,1000); var binded360 = false; $("textarea#stepcodeTextarea360").keyup(function(){ if(!binded360){ $(window).bind('beforeunload',function(){ binded360 = true; return "xxxx"; }); } var newCode = $(this).val() code360 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code360!=newCode){ // window.frames["iframe360"].document.write("
"); // window.frames["iframe360"].document.write(decodeHtml($("textarea#stepcodeTextarea360").val())); // window.frames["iframe360"].document.close(); // $(window.frames["iframe360"]).load(function(){ // $("#iframe360").height($("#iframe360").contents().find("body").height()+showittryitheight); // }); // code360 = newCode; // } }); $(".tryButton360").click(function(){ $("#tryDiv360").show(); $("#stepcodeTextarea360").focus(); $("#stepcodeTextarea360").height(200); $("#iframe360").height(0); window.frames["iframe360"].document.write("
"); window.frames["iframe360"].document.write(decodeHtml($("textarea#stepcodeTextarea360").val())); window.frames["iframe360"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe360"]).load(function(){ $("#iframe360").height($("#iframe360").contents().find("body").height()+showittryitheight); }); $("#iframe360").height($("#iframe360").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor360.focus(); editor360.setSize(null, "250"); $("#rendering360").hide(); $("#rendered360").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor360 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea360"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor360.on("change",function(doc){ if(!binded360){ $(window).bind('beforeunload',function(){ binded360 = true; return "xxxx"; }); } var newCode = doc.getValue(); code360 = newCode; $("textarea#stepcodeTextarea360").val(newCode); if(alreadyWriteCode360!=code360){ lastModifedTime360 = new Date().getTime(); $("#rendering360").show(); $("#rendered360").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor360 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor360.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv360").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

method="get"

使用method="get" 提交数据 是常用的提交数据的方式
如果form元素没有提供method属性,默认就是get方式提交数据
get方式的一个特点就是,可以在浏览器的地址栏看到提交的参数,即便是密码也看得到
"); window.frames["iframe_show1233"].document.write(decodeHtml($("textarea#stepcodeTextarea1233").val())); window.frames["iframe_show1233"].document.close(); $(window.frames["iframe_show1233"]).load(function(){ $("#iframe_show1233").height($("#iframe_show1233").contents().find("body").height()+showittryitheight); }); $("#iframe_show1233").height($("#iframe_show1233").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
method="get"
<form method="get" action="http://127.0.0.1/study/login.jsp"> 账号:<input type="text" name="name"> <br/> 密码:<input type="password" name="password" > <br/> <input type="submit" value="登陆"> </form>
<form method="get" action="http://127.0.0.1/study/login.jsp">
账号:<input type="text" name="name"> <br/>
密码:<input type="password" name="password" > <br/>
<input type="submit" value="登陆">
</form>
"); window.frames["iframe1233"].document.write(decodeHtml(code1233)); window.frames["iframe1233"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1233"]).load(function(){ $("#iframe1233").height($("#iframe1233").contents().find("body").height()+showittryitheight); }); $("#iframe1233").height($("#iframe1233").contents().find("body").height()+showittryitheight); alreadyWriteCode1233 = code1233; $("#rendering1233").hide(); $("#rendered1233").show(); } var tRereshRetry2DemoPanel1233 = setInterval(rereshRetry2DemoPanel1233,1000); var binded1233 = false; $("textarea#stepcodeTextarea1233").keyup(function(){ if(!binded1233){ $(window).bind('beforeunload',function(){ binded1233 = true; return "xxxx"; }); } var newCode = $(this).val() code1233 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1233!=newCode){ // window.frames["iframe1233"].document.write("
"); // window.frames["iframe1233"].document.write(decodeHtml($("textarea#stepcodeTextarea1233").val())); // window.frames["iframe1233"].document.close(); // $(window.frames["iframe1233"]).load(function(){ // $("#iframe1233").height($("#iframe1233").contents().find("body").height()+showittryitheight); // }); // code1233 = newCode; // } }); $(".tryButton1233").click(function(){ $("#tryDiv1233").show(); $("#stepcodeTextarea1233").focus(); $("#stepcodeTextarea1233").height(200); $("#iframe1233").height(0); window.frames["iframe1233"].document.write("
"); window.frames["iframe1233"].document.write(decodeHtml($("textarea#stepcodeTextarea1233").val())); window.frames["iframe1233"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1233"]).load(function(){ $("#iframe1233").height($("#iframe1233").contents().find("body").height()+showittryitheight); }); $("#iframe1233").height($("#iframe1233").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1233.focus(); editor1233.setSize(null, "250"); $("#rendering1233").hide(); $("#rendered1233").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1233 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1233"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1233.on("change",function(doc){ if(!binded1233){ $(window).bind('beforeunload',function(){ binded1233 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1233 = newCode; $("textarea#stepcodeTextarea1233").val(newCode); if(alreadyWriteCode1233!=code1233){ lastModifedTime1233 = new Date().getTime(); $("#rendering1233").show(); $("#rendered1233").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1233 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1233.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1233").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

method="post"

使用method="post" 也可以提交数据
post不会在地址栏显示提交的参数
如果要提交二进制数据,比如上传文件,必须采用post方式
"); window.frames["iframe_show1234"].document.write(decodeHtml($("textarea#stepcodeTextarea1234").val())); window.frames["iframe_show1234"].document.close(); $(window.frames["iframe_show1234"]).load(function(){ $("#iframe_show1234").height($("#iframe_show1234").contents().find("body").height()+showittryitheight); }); $("#iframe_show1234").height($("#iframe_show1234").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
method="post"
<form method="post" action="http://127.0.0.1/study/login.jsp"> 账号:<input type="text" name="name"> <br/> 密码:<input type="password" name="password" > <br/> <input type="submit" value="登陆"> </form>
<form method="post" action="http://127.0.0.1/study/login.jsp">
账号:<input type="text" name="name"> <br/>
密码:<input type="password" name="password" > <br/>
<input type="submit" value="登陆">
</form>
"); window.frames["iframe1234"].document.write(decodeHtml(code1234)); window.frames["iframe1234"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1234"]).load(function(){ $("#iframe1234").height($("#iframe1234").contents().find("body").height()+showittryitheight); }); $("#iframe1234").height($("#iframe1234").contents().find("body").height()+showittryitheight); alreadyWriteCode1234 = code1234; $("#rendering1234").hide(); $("#rendered1234").show(); } var tRereshRetry2DemoPanel1234 = setInterval(rereshRetry2DemoPanel1234,1000); var binded1234 = false; $("textarea#stepcodeTextarea1234").keyup(function(){ if(!binded1234){ $(window).bind('beforeunload',function(){ binded1234 = true; return "xxxx"; }); } var newCode = $(this).val() code1234 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1234!=newCode){ // window.frames["iframe1234"].document.write("
"); // window.frames["iframe1234"].document.write(decodeHtml($("textarea#stepcodeTextarea1234").val())); // window.frames["iframe1234"].document.close(); // $(window.frames["iframe1234"]).load(function(){ // $("#iframe1234").height($("#iframe1234").contents().find("body").height()+showittryitheight); // }); // code1234 = newCode; // } }); $(".tryButton1234").click(function(){ $("#tryDiv1234").show(); $("#stepcodeTextarea1234").focus(); $("#stepcodeTextarea1234").height(200); $("#iframe1234").height(0); window.frames["iframe1234"].document.write("
"); window.frames["iframe1234"].document.write(decodeHtml($("textarea#stepcodeTextarea1234").val())); window.frames["iframe1234"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1234"]).load(function(){ $("#iframe1234").height($("#iframe1234").contents().find("body").height()+showittryitheight); }); $("#iframe1234").height($("#iframe1234").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1234.focus(); editor1234.setSize(null, "250"); $("#rendering1234").hide(); $("#rendered1234").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1234 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1234"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1234.on("change",function(doc){ if(!binded1234){ $(window).bind('beforeunload',function(){ binded1234 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1234 = newCode; $("textarea#stepcodeTextarea1234").val(newCode); if(alreadyWriteCode1234!=code1234){ lastModifedTime1234 = new Date().getTime(); $("#rendering1234").show(); $("#rendered1234").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1234 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1234.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1234").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 4 :

get和post的区别

get和post的区别
get
是form默认的提交方式
如果通过一个超链访问某个地址,是get方式
如果在地址栏直接输入某个地址,是get方式
提交数据会在浏览器显示出来
不可以用于提交二进制数据,比如上传文件
post
必须在form上通过 method="post" 显示指定
提交数据不会在浏览器显示出来
可以用于提交二进制数据,比如上传文件


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-HTML-表单 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图