我有两个文字标签:
<div>
<input type="text" id="textfield1" size="5"/>
</div>
<div>
<input type="text" id="textfield2" size="5"/>
</div>
Run Code Online (Sandbox Code Playgroud)
我想有一个名为"Clear"的按钮可以删除这两个字段.
据我所知,我知道要实现一个按钮我应该使用这样的代码:
<div>
<input type="button" value="Clear Fields" onclick=SOMETHING />
</div>
Run Code Online (Sandbox Code Playgroud)
关于如何实施的任何线索SOMETHING
?
Bil*_*oat 27
如何只是一个简单的重置按钮?
<form>
<input type="text" id="textfield1" size="5">
<input type="text" id="textfield2" size="5">
<input type="reset" value="Reset">
</form>
Run Code Online (Sandbox Code Playgroud)
sur*_*lit 24
一个简单的JavaScript函数就可以完成这项工作.
function ClearFields() {
document.getElementById("textfield1").value = "";
document.getElementById("textfield2").value = "";
}
Run Code Online (Sandbox Code Playgroud)
只需按下按钮即可:
<button type="button" onclick="ClearFields();">Clear</button>
Run Code Online (Sandbox Code Playgroud)
如果要重置它,那么简单使用:
<input type="reset" value="Reset" />
Run Code Online (Sandbox Code Playgroud)
但要注意,它不会清除具有默认值的文本框value
.例如,如果我们有以下文本框,默认情况下,它们具有以下值:
<input id="textfield1" type="text" value="sample value 1" />
<input id="textfield2" type="text" value="sample value 2" />
Run Code Online (Sandbox Code Playgroud)
所以,要清除它,用javascript补充它:
function clearText()
{
document.getElementById('textfield1').value = "";
document.getElementById('textfield2').value = "";
}
Run Code Online (Sandbox Code Playgroud)
并将其附加到onclick
重置按钮:
<input type="reset" value="Reset" onclick="clearText()" />
归档时间: |
|
查看次数: |
167985 次 |
最近记录: |