您的位置:首页 > Web前端 > JavaScript

如何用图片实现表单(form)的重置(reset)按钮

2008-04-29 11:34 591 查看
一个简单包含提交、重置按钮的表单代码如下:HTML: <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 来避免默认的提交操作:HTML: <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> 关键代码:Code: onclick="javascript:document.forms['testForm_2'].reset(); return false;" document.forms['testForm_2'].reset(); 是将名称为 testForm_2 的表单重置。return false; 是防止提交表单。(2)直接用图片模拟的重置按钮HTML: <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> 关键代码:Code: onclick="javascript:document.forms['testForm_3'].reset(); "style="cursor:pointer;" document.forms['testForm_2'].reset(); 是将名称为 testForm_2 的表单重置。style="cursor:pointer;" 设置图片悬停时,显示手型光标。两种方法大同小异,在 javascript 被禁止的情况下,第二种方法按钮不会执行任何操作,而第一种方法却会执行提交操作。当然,我们也可以直接把样式交给 CSS 去处理,用背景图的方式来实现,但是这样需要将 value 的值留空,如果 CSS 被禁止,那么将会显示一个没有文字的按钮,每个方法都有自己的问题,可以根据自己的需要来选择解决方案。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息