Javascript Javascript在按钮单击时清除表单字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6750445/
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-23 23:01:40  来源:igfitidea点击:

Javascript to clear form field on button click

javascriptgoogle-maps-api-3user-inputtextinput

提问by IRHM

I have a field called 'inputaddress' which a user types address details into, so they can perform a google map geocode.

我有一个名为“inputaddress”的字段,用户可以在其中输入地址详细信息,以便他们可以执行谷歌地图地理编码。

What I would like to do is when the user clicks on the 'searchfortheaddress' button, it will perform the geocode but will then clear the 'inputaddress' field.

我想做的是当用户单击“搜索地址”按钮时,它将执行地理编码,但随后会清除“输入地址”字段。

Could someone perhaps tell me please how I can clear the field.

有人可以告诉我如何清除该字段。

回答by Barry Kaye

document.getElementById("inputaddress").value = '';

回答by Eamorr

Would $('#inputaddress').html("");or document.getElementById('inputaddress').value=""work?

$('#inputaddress').html("");document.getElementById('inputaddress').value=""工作?

回答by psynnott

document.getElementById("inputaddress").value = '';

回答by lovesh

i am assuming that u id your input address field addrjust add this code to the onclickevent of your searchforaddressbutton

我假设您确定了您的输入地址字段,addr只需将此代码添加到onclick您的searchforaddress按钮事件中

document.getElementById('addr').value="";

like this (i am assuming is of ur searchforaddressbutton is searchforaddress)

像这样(我假设是你的searchforaddress按钮是searchforaddress

    document.getElementById('searchforaddress').onclick=function() {
    document.getElementById('addr').value="";
};