javascript 删除输入字段值 onclick

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

Remove input filed value onclick

javascriptjqueryhtml

提问by Affan Ahmad

I want to remove input filed value while click on button.How can i do that.

我想在单击按钮时删除输入的字段值。我该怎么做。

For example

例如

<input type="text" name="text"> /***************IF value =Akram ************/
<input type="button">

采纳答案by Kris Hollenbeck

HTML:

HTML:

<input type="text" name="text" value="Akram">
<input type="text" name="text" value="something else">

<input type="submit" value="button" id="btn" />

jQuery:

jQuery

$('#btn').click(function(){
    $('input').each(function(){
      if ($(this).val() == "Akram")
        $(this).val('');
    }); 
});

DEMO:

演示:

http://jsfiddle.net/e7j24/5/

http://jsfiddle.net/e7j24/5/

回答by Olaf Dietsche

You can use an html only solution, when you put your input elements in a form

当您将输入元素放入表单时,您可以使用仅 html 的解决方案

<form>
    <input type="text" name="text" />
    <input type="reset" />
</form>

JSFiddle

JSFiddle

And here is a Javascript version

这是一个 Javascript 版本

<input id="text" type="text" name="text" />
<input id="button" type="button" />
var text = document.getElementById('text');
var button = document.getElementById('button');
button.onclick = function() {
    text.value = '';
}

JSFiddle

JSFiddle

回答by Ringo

Try this:

试试这个:

$(function(){
  $('input[type=button]').click(function(){
    if($('input[type=text]').val() == 'something') 
       $('input[type=text]').val('');
  });
});

回答by user202172

<input type="text" name="text" id="input" />
<input type="button" onclick="if($('#input').val() == 'Akram'){ $('#input').val(''); }" />