[英]Looping through textboxes 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> Add</button>
</form>
</div>
我正在做以下事情,但还没有完成。
$("#RegexInsert .inputs").each(function () {
$('input').val('');
});
亲切的问候
您不需要循环,可以在单个选择器中执行:
$('.form-control').val('');
我不确定你的示例中的.inputs
来自哪里,因为它不在你的HTML中。 如果需要,您可以使选择器更通用:
$('#RegexInsert input[type="text"]').val('');
要清除输入元素,如其他地方所述:
$("#RegexInsert .inputs").val('');
要重置输入元素的值:
$("#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小提琴演示 。
您不必使用循环
$("#RegexInsert input").val('');
您可以执行以下操作:
$("#RegexInsert input").each(function () {
$(this).val(); // Gets the value
$(this).val('newValue'); // Sets the value
});
在您的情况下,您有一个快捷方式来清除所有(每个()隐式):
$("#RegexInsert input").val('');
请注意,因为这将清除任何具有value属性的输入。 你可能想要考虑更强的选择器,比如$("#RegexInsert input[type=text]")
这样做:
$('#RegexInsert input.form-control').val('');
要么:
$('.form-group input.form-control').val('');
你的原始代码
$("#RegexInsert .inputs").each(function () {
$('input').val('');
});
.inputs不存在于HTML中的任何位置。 如果您指的是输入标签,则应输入您的选择器。
$("#RegexInsert input").each(function () {
$(this).val('');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.