简体   繁体   English

使用jquery循环遍历文本框

[英]Looping through textboxes using jquery

How can I go about in clearing these textboxes by looping through them using Jquery? 如何通过使用Jquery循环来清除这些文本框?

<div class="col-md-4" id="RegexInsert">
    <h4>New Regex Pattern</h4>
    <form role="form">
        <div class="form-group">
            <label for="firstName">Desription</label>
            <input type="text" class="form-control" id="txtRegexDesription" placeholder="Description" />
        </div>
        <div class="form-group">
            <label for="firstName">Regex pattern</label>
            <input type="text" class="form-control" id="txtRegexPattern" placeholder="Regex pattern(C#)" />
        </div>
        <div class="form-group">
            <label for="firstName">Data type</label>
            <input type="text" class="form-control" id="txtDataType" placeholder="Data type" />
        </div>
        <button type="button" class="btn btn-default btn-sm btnAddRegexPattern" data-applicationid=""><span class="glyphicon glyphicon-plus"></span>&nbsp;Add</button>
    </form>
</div>

I am doing the following which is not quite there yet. 我正在做以下事情,但还没有完成。

$("#RegexInsert .inputs").each(function () {
    $('input').val('');
});

kind regards 亲切的问候

You don't need a loop, you can do it in a single selector: 您不需要循环,可以在单个选择器中执行:

$('.form-control').val('');

I'm not sure where .inputs in your example is coming from as it is not in your HTML. 我不确定你的示例中的.inputs来自哪里,因为它不在你的HTML中。 You can make the selector more generic if required: 如果需要,您可以使选择器更通用:

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

To clear the input elements, as noted elsewhere: 要清除输入元素,如其他地方所述:

$("#RegexInsert .inputs").val('');

To reset the input elements' values: 重置输入元素的值:

$("#RegexInsert .inputs").val(function(){
    return this.defaultValue;
});

Or, if you have multiple input-types (and this is, frankly, more complex than I at first assumed it would be...): 或者,如果你有多种输入类型(坦率地说,这比我最初认为的更复杂......):

$('#reset').on('click', function(e){
    e.preventDefault();
    $('input, textarea, select option')
    .each(function(){
        var tag = this.tagName.toLowerCase(),
            type = this.type,
            prop, value;
        if ('undefined' !== typeof type) {
            switch (type) {
                case 'radio':
                case 'checkbox':
                    prop = 'checked';
                    break;
                case 'text':
                case 'textarea': // textarea elements seem to have a type
                                 // property, undexpectedly (in Chrome/Win XP)
                    prop = 'value';
                    break;
            }
        }
        else {
            switch (tag) {
                case 'option':
                    prop = 'selected';
                    break;
                case 'textarea':    // left this just in case the 'type' property
                    prop = 'value'; // is a Chrome/Webkit specific thing.
                    break;
            }
        }
        this[prop] = this['default' + prop.replace(/^./,function(a){
            return a.toUpperCase();
        })];
    });
});

JS Fiddle demo . JS小提琴演示

您不必使用循环

$("#RegexInsert input").val('');

You can do the following : 您可以执行以下操作:

$("#RegexInsert input").each(function () {
    $(this).val(); // Gets the value
    $(this).val('newValue'); // Sets the value
});

In your case, you have a shortcut to clean all (the each() is implicit) : 在您的情况下,您有一个快捷方式来清除所有(每个()隐式):

$("#RegexInsert input").val('');

Beware though as this will clean any input that has a value attribute. 请注意,因为这将清除任何具有value属性的输入。 You may want to consider stronger selector, like $("#RegexInsert input[type=text]") 你可能想要考虑更强的选择器,比如$("#RegexInsert input[type=text]")

do like this: 这样做:

$('#RegexInsert input.form-control').val('');

or: 要么:

$('.form-group input.form-control').val('');

your original code 你的原始代码

$("#RegexInsert .inputs").each(function () {
    $('input').val('');
});

.inputs doesn't exist anywhere in your HTML. .inputs不存在于HTML中的任何位置。 If you are referring to the input tags, the your selector should be input. 如果您指的是输入标签,则应输入您的选择器。

$("#RegexInsert input").each(function () {
    $(this).val('');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM