html submit 提交怎么用?

html-css07

html submit 提交怎么用?,第1张

最常见的就是type="submit"的按钮触发form的onsubmit事件。

<scripttype="text/javascript">

   function validateForm(){

     if(document.reply.title.value== ""){ //通过form名来获取form

            alert("pleaseinput the title!")

            document.reply.title.focus()

            returnfalse

     }   

     if(document.forms[0].cont.value== ""){ //通过forms数组获取form

            alert("pleaseinput the content!")

            document.reply.cont.focus()

            returnfalse

     }

     returntrue

 }

<formname="reply"  method="post" onsubmit="returnvalidateForm( )">

       <input type="text"name="title" size="80" /><br />

       <textarea name="cont"cols="80" rows="12"></textarea><br />

       <input type="submit"value="提交" >

</form>

表格:table

我们做数据统计的时候,都需要用到表格,table就是用来定义一个表格的,我们来设置一个表格:

<html>

<head>

<title>网站名称</title>

</head>

<body>

<table border="1">

<caption>我是表格标题</caption>

<thead>

<th>姓名</th>

<th>性别</th>

<th>联系地址</th>

</thead>

<tfoot>

<tr>

<td colspan="3">我是一个底部</td>

</tr>

</tfoot>

<tbody>

<tr>

<td>小李</td>

<td>男</td>

<td>哈哈哈</td>

</tr>

<tr>

<td>小韩</td>

<td>女</td>

<td>美美</td>

</tr>

<tr>

<td>小张</td>

<td>男</td>

<td>哦哦哦</td>

</tr>

</tbody>

</table>

</body>

</html>

注:表格相关的元素很多,我们这里整理一下:

caption:用来设置表格标题;

thead:表示表格的头部区域;

th:表示表格的头部中的一个列;

tbody:表示表格的中间主要显示区域;

tr:表示表格的一个行;

td:表示表格的和个列;

tfoot:设置表格的脚部;

colspan:表示横跨几列;

表单:form

表单也是我们开发中经常用到的一个元素,我们登录时都是使用表单来向后台发送请求的,我们创建一个登录界面:

<html>

<head>

<title>网站名称</title>

</head>

<body>

<form action="/demo.php" method="get">

<div>

<input type="text" name="username"/>

</div>

<div>

<input type="password" name="password"/>

</div>

<button type="submit">登录</button>

</form>

</body>

</html>

注:form表单的元素介绍如下:

input:表示一个输入框,type属性用来表示此输入框的功能,text时表示输入的可见文件;password表示输入的是不可见到的密码;name属性用来设置后台语言用来获取此输入框的内容;

button:表示一个按钮,type:submit表示点击时会自动提交;提交到哪里呢?又是以什么方式提交呢?

我们看下form中的两个属性:

action:用来定义提交到的地址;

method:表示提交到后台语言时的提交方式,get或post,其他的还有put,delete

html中submit和button二者都以按钮的形式展现,看起来都是按钮,所不同的是type属性和处发响应的事件上,submit会提交表单,button不会提交表单。

submit和button两者主要区别在于:

1、submit默认为form提交,可以提交表单(form)。submit其实是button的一个特例,也是button的一种,它把提交这个动作自动集成了。

2、button作为普通的按钮则响应用户自定义的事件,如果不指定onclick等事件处理函数,它是不做任何事情,不会自动提交表单数据。当然,button也可以通过JS代码完成表单提交的工作。比如: onclick="document.form1.submit()"。

3、如果表单在点击提交按钮后需要用JS进行处理(包括输入验证)后再提交的话,通常都必须把submit改成button,即取消其自动提交的行为,否则,将会造成提交两次的效果,对于动态网页来说,也就是对数据库操作两次。或者在使用submit时验证时加return true或false。