重置HTML表单中的特定输入元素 [英] Reset Particular Input Element in a HTML Form

查看:143
本文介绍了重置HTML表单中的特定输入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页中有多个输入文字框。如果某些条件失败,我想将特定的文本框重置为onload状态。
我正在考虑使用Hidden元素来存储文本框的onload状态。我会提出其他建议或解决方案来解决这个问题。

I have multiple input textboxes in my page. I want to reset particular text box to its onload state if certain conditions fails. I am considering using a Hidden element to store the onload state of the textbox. I would other suggestions or solutions to resolve this issue.

推荐答案

defaultValue

<input type="text" value="initial" id="field">
<button id="reset">reset</button>
<script type="text/javascript">
    document.getElementById('reset').onclick= function() {
        var field= document.getElementById('field');
        field.value= field.defaultValue;
    };
</script>

可用于文字密码 textarea ,加上 defaultChecked 复选框 / 电台 defaultSelected 选项。奇怪的是,在隐藏 可用(除IE外,它被认为是一个bug)。

Available on text, password and textarea, plus defaultChecked on checkbox/radio and defaultSelected on option. Curiously, not available on hidden (except in IE, where it's considered a bug).

这篇关于重置HTML表单中的特定输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆