JQuery 选择除隐藏之外的输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2396846/
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
JQuery Select Input Fields Except Hidden
提问by Bob
I have a row in a table which contains a checkbox and some other form fields (text boxes, hidden fields, select lists). When the checkbox is checked I want to disable all form fields in that row except for the hidden fields. I have this working for the most part, but I can't seem to ignore the hidden fields.
我在表格中有一行,其中包含一个复选框和一些其他表单字段(文本框、隐藏字段、选择列表)。当复选框被选中时,我想禁用该行中除隐藏字段之外的所有表单字段。我大部分时间都在使用这个,但我似乎无法忽略隐藏的字段。
What is the best way to select all form fields in a table row but ignore hidden fields in the selection?
选择表格行中的所有表单字段但忽略选择中的隐藏字段的最佳方法是什么?
回答by neiker
$(":input:not([type=hidden])")
":input" is a jQuery pseudo selector... "Selects all input, textarea, select and button elements." http://api.jquery.com/input-selector/
":input" 是一个 jQuery 伪选择器... "选择所有输入、文本区域、选择和按钮元素。" http://api.jquery.com/input-selector/
回答by cletus
Assuming by "hidden" you mean type="hidden"
ie:
假设“隐藏”是指type="hidden"
:
<input type="hidden" name="foo" value="bar">
then you can use the attribute not equals selectorto do this:
那么你可以使用属性不等于选择器来做到这一点:
$("tr input:checkbox").click(function() {
var cb = $(this);
var tr = $(this).closest("tr");
if (cb.val()) {
tr.find("input[type!='hidden']").attr("disabled", true);
} else {
tr.find("input[type!='hidden']").removeAttr("disabled");
}
});
My general advice is avoid attribute selectors. They're slow. Give the relevant inputs (either the hidden ones or the not hidden ones) a class and then use that in the selectors.
我的一般建议是避免使用属性选择器。他们很慢。给相关的输入(隐藏的或非隐藏的)一个类,然后在选择器中使用它。
If however you mean "hidden" as in "not visible" then use the :visible
selector:
但是,如果您的意思是“隐藏”如“不可见”,则使用:visible
选择器:
$("tr input:checkbox").click(function() {
var cb = $(this);
var tr = $(this).closest("tr");
if (cb.val()) {
tr.find("input:visible").attr("disabled", true);
} else {
tr.find("input:visible").removeAttr("disabled");
}
});
回答by pmereles
You can also solve this using the hidden selector
您也可以使用隐藏选择器解决此问题
$("input:not(:hidden)")
$("input:not(:hidden)")
See also: jQuery hidden selector
另请参阅:jQuery 隐藏选择器
回答by Sunil
I found a simple way of doing this in jquery.
我在 jquery 中找到了一种简单的方法。
$("tr input").not("input[type='hidden']")
In code snippet below, there is a div with an id of tabsDiv
that contains multiple html elements and this code gets all input elements inside this div except those with type of hidden
.
在下面的代码片段中,有一个 id 为 的 divtabsDiv
包含多个 html 元素,此代码获取此 div 中的所有输入元素,但类型为hidden
.
$("#tabsDiv").find("input").not("input[type='hidden']")
回答by Sarfraz
.........
…………
$('tr input').attr('disabled', true)
$('tr input[type="hidden"]').removeAttr('disabled')
回答by Marko Dumic
First, you need to tag your "selector" checkbox (e.g. with a class attribute "selector) so that it is clear that it is used for checking instead of regular form element. Then you use this:
首先,您需要标记您的“选择器”复选框(例如使用类属性“选择器”),以便很明显它用于检查而不是常规表单元素。然后您使用这个:
$('table :checkbox.selector').click(function(ev) {
$(ev.currentTarget)
.parents('td').siblings('td')
.find(':input:not([type=hidden])')
.attr('disabled', ev.currentTarget.checked);
});
This solution works for all inputs (eg. select lists, textareas) and it doesn't disable the selector checkbox itself.
此解决方案适用于所有输入(例如选择列表、文本区域),并且它不会禁用选择器复选框本身。
I'm assuming you use latest jQuery.
我假设您使用最新的 jQuery。
回答by Engineer
Just add a filter of visible which means it will take only input element which are visible to users.
This worked for me I am adding description for 30 char.
只需添加一个可见的过滤器,这意味着它将只采用用户可见的输入元素。
这对我有用,我正在添加 30 个字符的描述。
$("input:visible")
$("输入:可见")