如何使用 Javascript 清除 HTML 上的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15969307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:28:27  来源:igfitidea点击:

How to clear textbox on HTML using Javascript

javascripthtmlbuttontextareaclear

提问by Adyana Permatasari

I have code in html like this

我有这样的 html 代码

<html>
<script type="text/javascript" src='LatihanKuisJs.js'></script>
    <body>
        <form name="kuis">
            <table border="1" width="50%">
                <tr>
                    <th colspan="2" >Celcius
                </tr>
                <tr>
                        <td align="center" width="80%">Kelvin</td>
                        <td align="center"><input type="text" id="kelvin">
                        </td>
                </tr>
                <tr>
                    <td align="center" width="80%">Reamur</td>
                    <td align="center"><input type="text" id="reamur"></td>
                </tr>
                <tr>
                    <td align="center" width="80%">Fahrenheit</td>
                    <td align="center"><input type="text" id="fahrenheit"></td>
                </tr>
            </table>
            <input type="button" value="Calculate" onclick='calculateCelcius();'/>
            <br/><br/>
            <textarea rows="20" cols="90" id="textarea">
            </textarea>
            <br/><br/>
            <input type="button" value="Clear" onclick='clear();'/>
        </form>
    </body>
</html>

and external javascript function like this:

和这样的外部 javascript 函数:

function calculateCelcius(){
    var kelvin = document.getElementById('kelvin');
    var reamur = document.getElementById('reamur');
    var fahrenheit = document.getElementById('fahrenheit');
    var textarea = document.getElementById('textarea');

    var hasil=(kelvin.value*1 + reamur.value*1 + fahrenheit.value*1);
    textarea.value += hasil + '\n';
}

function clear(){
    document.getElementById("textarea").value="";
}

When I tried to click the clear button on my page, the text area wasn't clear. What's wrong? And what should I do?

当我尝试单击页面上的清除按钮时,文本区域不清晰。怎么了?我该怎么办?

回答by VisioN

Just rename your function from clearto something like clearTextareaand it will work.

只需将您的函数重命名clear为类似的名称clearTextarea,它就会起作用。

The clear()method refers to obsolete document.clear()method, which is described at MDNas:

clear()方法指的是过时的document.clear()方法,在MDN 中描述为:

This method used to clear the whole specified document in early (pre-1.0) versions of Mozilla. In recent versions of Mozilla-based applications as well as in Internet Explorer and Netscape 4 this method does nothing.

此方法用于清除 Mozilla 早期(1.0 之前)版本中的整个指定文档。在基于 Mozilla 的应用程序的最新版本以及 Internet Explorer 和 Netscape 4 中,此方法没有任何作用。

Also according to HTML5 specification:

同样根据HTML5 规范

The clear()method must do nothing.

clear()方法必须什么都不做。

References:

参考:

  1. https://developer.mozilla.org/en-US/docs/DOM/document.clear
  2. http://www.whatwg.org/specs/web-apps/current-work/multipage/obsolete.html#dom-document-clear
  1. https://developer.mozilla.org/en-US/docs/DOM/document.clear
  2. http://www.whatwg.org/specs/web-apps/current-work/multipage/obsolete.html#dom-document-clear

回答by user3232983

if you use a function like this one

如果你使用这样的功能

function clearInput(element){
element.value="";
}

then in the input add this

然后在输入中添加这个

onfocus="clearInput(this)"

this can be used multiple times for any text fields or text areas because the id of the object is passed where it calls the function from.

这可以多次用于任何文本字段或文本区域,因为对象的 id 是从它调用函数的地方传递的。

RKillah

基拉

回答by Ali Shah Ahmed

Try adding javascript: before your function name when defining onclick event.
Something like this:

尝试在定义 onclick 事件时在函数名称之前添加 javascript: 。
像这样的东西:

<input type="button" value="Clear" onclick='javascript: clear();'/>