jQuery 清除 Div 中的值

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

jQuery clear Values in a Div

jqueryhtmlclear

提问by DiegoP.

I have a DIV that contains many input text.

我有一个包含许多输入文本的 DIV。

I need a way in jQuery 1.3.2 to clear all the values inside the inputs onclick.

我需要在 jQuery 1.3.2 中清除 onclick 输入中的所有值。

So when I click on a specific link all the values of the inputs inside that DIV will be cleared.

因此,当我单击特定链接时,该 DIV 内的所有输入值都将被清除。

I do not have any sample code, I just need to know if there is a way to clear all the values of inputs that are inside a specific DIV (not in a FORM, but in a DIV).

我没有任何示例代码,我只需要知道是否有办法清除特定 DIV(不是在 FORM 中,而是在 DIV 中)内的所有输入值。

Thank you

谢谢

回答by mcgrailm

yes there is

就在这里

html like this

像这样的html

<div id="div_id">
    <input type="text" value="foo" />
    <input type="text" value="foo" />
    <input type="text" value="foo" />
    <input type="text" value="foo" />
</div>

then jQuery

然后是jQuery

$('#div_id input[type="text"]').val('');

working demo

工作演示

回答by amit_g

What are those inputs? Textboxes? May be this

这些输入是什么?文本框?可能是这个

$("#DivID input:text").val("");

Demo

演示

回答by Udhayakumar

To clear form element values inside the div use below

要清除 div 内的表单元素值,请使用下面的

 function clear_form_elements(id_name) {
  jQuery("#"+id_name).find(':input').each(function() {
    switch(this.type) {
        case 'password':
        case 'text':
        case 'textarea':
        case 'file':
        case 'select-one':       
            jQuery(this).val('');
            break;
        case 'checkbox':
        case 'radio':
            this.checked = false;
    }
  });
}

回答by Chandu

Try using val proerpty of the input text elements to blank.

尝试使用输入文本元素的 val 属性为空。

Something like:

就像是:

$("input[type='text']", "#<YOUR_DIV_ID>").val("");

e.g:

例如:

<div id="textDiv">
    <input type="text" Value="1"/> <br/>
    <input type="text" Value="2"/> <br/>
    <input type="text" Value="3"/> <br/>
    <input type="text" Value="4"/> <br/>
    <input type="text" Value="5"/> <br/>
    <input type="text" Value="6"/> <br/>
    <a name="clickMe" href="javascript:void(0)">Empty Boxes</a>
</div>

<script type="text/javascript">
    $(function(){
     $("a[name='clickMe']").click(function(){
        $("input[type='text']", "#textDiv").val("");
     });
    });
</script>

Check live example @: http://jsfiddle.net/DKwy8/

检查现场示例@:http: //jsfiddle.net/DKwy8/

回答by Jeremy B.

$('linkselector').onClick(function() {
    $('#DivId input').val('');
});

回答by diEcho

try

尝试

HTML

HTML

<div id="myDiv">
    <input type="text" value="One">
    <input type="text" value="Two">
    <input type="text" value="Three">
</div>
<a href="#" name="ClearDiv" id="clearDiv">Clear</a>

jQuery

jQuery

$('a#clearDiv').bind('click', function() {        
    var $div = $('div#myDiv');
    $('input[type="text"]', $div).each(function() {     
      $(this).val('');
    });

});

working DEMO

工作演示