使用jquery循环遍历文本框

时间:2014-05-09 06:53:59

标签: javascript 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>

我正在做以下事情,但还没有完成。

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

亲切的问候

6 个答案:

答案 0 :(得分:11)

您不需要循环,可以在单个选择器中执行:

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

我不确定您示例中的.inputs来自哪里,因为它不在您的HTML中。如果需要,您可以使选择器更通用:

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

答案 1 :(得分:6)

清除输入元素,如其他地方所述:

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

重置输入元素&#39;值:

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

或者,如果你有多种输入类型(坦率地说,这比我最初想象的要复杂得多......):

$('#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

答案 2 :(得分:4)

您不必使用循环

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

答案 3 :(得分:4)

您可以执行以下操作:

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

在您的情况下,您有一个清除所有的快捷方式(每个()是隐式的):

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

请注意,因为这会清除任何具有value属性的输入。您可能需要考虑更强的选择器,例如$("#RegexInsert input[type=text]")

答案 4 :(得分:3)

这样做:

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

或:

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

答案 5 :(得分:0)

您的原始代码

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

.inputs不存在于HTML中的任何位置。如果您指的是输入标签,则应输入您的选择器。

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