[英]How to clear a input array all elements using jQuery
I have defined my form elements using the html array method like this: 我使用html数组方法定义了我的表单元素,如下所示:
<input type="text" maxlength="45" name="Apikey[key]">
<select name="Apikey[developer]">
How can I clear all Apikey array using jQuery? 如何使用jQuery清除所有Apikey数组?
The question is how to clear them? 问题是如何清除它们? All possible forms fields must be cleared.
必须清除所有可能的表单字段。
Try ^(starts with) in the selector instead of |. 在选择器中尝试^(以...开头)而不是|。 Also to reset the fields appropriately storing the initial values using data.
还要使用数据重置适当存储初始值的字段。 ie:
即:
$(function(){
$("input[name^=Apikey]").each(function(){
var $this = $(this);
$this.data("initial-val", $this.val());
});
});
.
.
.
function resetApiValues(){
$("input[name^=Apikey]").each(function(){
var $this = $(this);
$this.val($this.data("initial-val"));
});
}
$('input[name^="Apikey"]')...
$( '输入[名称^ = “Apikey”]')...
See the official jQuery API here. 请在此处查看官方jQuery API 。 bye
再见
The easiest thing to do would be to use the "standard" form plugin and call clearForm()
or clearFields()
: 最简单的方法是使用“标准”表单插件并调用
clearForm()
或clearFields()
:
$('#formId').clearForm();
$('#formId [name^=Apikey]').clearFields();
Or, if you don't want or need all the extra form plugin machinery, just grab the source and figure out how clearForm()
and clearFields()
work. 或者,如果您不想要或不需要所有额外的表单插件机制,只需抓住源代码并找出
clearForm()
和clearFields()
工作原理。 clearFields()
is actually quite simple: clearFields()
实际上非常简单:
$.fn.clearFields = $.fn.clearInputs = function() {
return this.each(function() {
var t = this.type, tag = this.tagName.toLowerCase();
if (t == 'text' || t == 'password' || tag == 'textarea') {
this.value = '';
}
else if (t == 'checkbox' || t == 'radio') {
this.checked = false;
}
else if (tag == 'select') {
this.selectedIndex = -1;
}
});
};
here you go: 干得好:
$("input[name^='Apikey']").attr("name", "");
to clear the values: 清除价值观:
$("input[name^='Apikey']").val("");
I see that you have edited to include different HTML elements, in that case: 我看到你已经编辑过包含不同的HTML元素,在这种情况下:
$("*[name^='Apikey']")).each(function() {
switch(this.type) {
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}
});
You cant just clear all the values. 你不能清除所有的价值观。 Some elements like SELECT have to have a value.
像SELECT这样的元素必须有一个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.