女生有暗财是什么意思:如何用图片实现表单(form)的重置(reset)按钮 | CSS教程 | CodeBit.cn
来源:百度文库 编辑:中财网 时间:2024/04/29 06:24:40
如何用图片实现表单(form)的重置(reset)按钮
发表于 2007 年 03 月 26 日 由 Artlover合理的运用图片,能够使站点更加丰富多彩,相信有许多喜欢用图片的设计师都遇到过一个问题:用图片实现表单(form)的重置(reset)按钮时,这个按钮并不能重置表单,相反却执行了提交表单的操作,本文说明了这个问题的原因,并给出了相应的解决方案。
出现上述问题的原因主要是:普通的按钮是通过类型来区分的,submit 为提交按钮,reset 为重置按钮,而类型为图片(type="image")的按钮,其默认操作是提交表单。因此,如果想用图片实现重置按钮,就需要一些额外的设置了。
如何用图片实现表单(form)的重置(reset)按钮 – 示例
一个简单包含提交、重置按钮的表单代码如下:
12345<
form
method
=
"post"
name
=
"testForm_1"
action
=
""
>
<
p
><
input
type
=
"text"
name
=
"keyword"
/>
p
>
<
input
type
=
"submit"
value
=
"Submit"
/>
<
input
type
=
"reset"
value
=
"Reset"
/>
form
>
如果想用图片代替重置按钮,可以用下面的方法:
(1)给 type 为 image 的 input 添加 onclick 事件来实现重置,并通过添加 return false 来避免默认的提交操作:
12345<
form
method
=
"post"
name
=
"testForm_2"
action
=
""
>
<
p
><
input
type
=
"text"
name
=
"keyword"
/>
p
>
<
input
type
=
"image"
src
=
"send.gif"
/>
<
input
type
=
"image"
src
=
"reset.gif"
onclick
=
"javascript:document.forms['testForm_2'].reset(); return false;"
/>
form
>
关键代码:
onclick="javascript:document.forms['testForm_2'].reset(); return false;"
document.forms['testForm_2'].reset(); 是将名称为 testForm_2 的表单重置。
return false; 是防止提交表单。
(2)直接用图片模拟的重置按钮
12345<
form
method
=
"post"
name
=
"testForm_3"
action
=
""
>
<
p
><
input
type
=
"text"
name
=
"keyword"
/>
p
>
<
input
type
=
"image"
src
=
"send.gif"
/>
<
img
src
=
"reset.gif"
alt
=
"Reset"
onclick
=
"javascript:document.forms['testForm_3'].reset();"
style
=
"cursor:pointer;"
/>
form
>
关键代码:
onclick="javascript:document.forms['testForm_3'].reset(); "style="cursor:pointer;"
document.forms['testForm_2'].reset(); 是将名称为 testForm_2 的表单重置。
style="cursor:pointer;" 设置图片悬停时,显示手型光标。
两种方法大同小异,在 javascript 被禁止的情况下,第二种方法按钮不会执行任何操作,而第一种方法却会执行提交操作。
当然,我们也可以直接把样式交给 CSS 去处理,用背景图的方式来实现,但是这样需要将 value 的值留空,如果 CSS 被禁止,那么将会显示一个没有文字的按钮,每个方法都有自己的问题,可以根据自己的需要来选择解决方案。