[英]select specific decendants using Jquery
In JQuery's Documentation the "parent > child"
selector selects all direct child elements specified by "child" of elements specified by "parent". 在JQuery的文档中 ,
"parent > child"
选择器选择“父”指定的元素的“子”指定的所有直接子元素。 "Direct child" is just the element one level down. “直子”只是向下一级的元素。 For example, I have this code:
例如,我有以下代码:
...
<table class="advanceSearch">
<tr>
<td>
<ul class="form">
<li>
<label>Document Type:</label>
<input type="text" id="cmbDocumentType" name="paramtitle" />
</li>
<li>
<label>Title:</label>
<input type="text" id="paramtitle" name="paramtitle" />
</li>
<li>
<label>Notes:</label>
<input type="text" id="paramNotes" name="paramNotes" />
</li>
<li>
<label>Revision Number:</label>
<input type="text" id="paramRevisionNumber" name="paramRevisionNumber" />
</li>
<li>
<label>Draft Number:</label>
<input type="text" id="paramDraftNumber" name="paramDraftNumber" />
</li>
<li>
<label>Version Number:</label>
<input type="text" id="paramVersionNumber" name="paramVersionNumber" />
...
I want to select the input
elements with ids starting with "param", and so I used the find()
instead of the parent > child
selector: 我想选择ID以“ param”开头的
input
元素,因此我使用了find()
而不是parent > child
选择器:
$("table.advanceSearch").find("ul.form").find('input[id^="param"]').attr("disabled", "disabled");
This works well but I find it redundant having two find()
selectors. 这很好用,但是我发现有两个
find()
选择器是多余的。 Is there any way to have the shorthand for this? 有什么办法可以简化吗?
You don't actually need .find()
at all; 实际上,您根本不需要
.find()
; you can use two descendant selectors instead. 您可以改用两个后代选择器。
$('table.advanceSearch ul.form input[id^="param"]')
Remember: x > y
(the child selector) is more-or-less interchangeable with $('x').children('y')
, and $('x y')
is more-or-less interchangeable with $('x').find('y')
. 请记住:
x > y
(子选择器)与$('x').children('y')
或多或少可以互换,而$('x y')
与$('x').find('y')
。
Try this: 尝试这个:
$('table.advanceSearch ul.form input[id^="param"]').prop('disabled', true);
OR 要么
$('table.advanceSearch ul.form').find('input[id^="param"]').prop('disabled', true);
OR 要么
$('table.advanceSearch ul.form li').children('input[id^="param"]').prop('disabled', true);
Your selector can be much longer: 您的选择器可以更长:
$(".advanceSearch input[id^='param']").prop("disabled", true);
This will do the same, without the verbosity. 无需冗长的操作即可完成此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.