0

With jQuery, I do the following

MyCompaniesInfo = $('input[name^="Companies"]');

And the console gives me this (Firebug):

MyCompaniesInfo


Object[input b1280bf5...73a2a334, input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__IsExist False, input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__Type Company, 29 more...]

0 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__Name.text-box
1 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__phone.text-box
2 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__address.text-box
3 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__street.text-box
4 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__anotherthing.text-box
5 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__another.text-box
6 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__blabla.text-box
7 input#Companies_b1280bf5-102e-4592-a1b1-53e973a2a334__loremipsum.text-box

8 input#Companies_b1280bf52a1b1-53e973a2a334__Name.text-box
9 input#Companies_b1280bf5a1b1-53e973a2a334__phone.text-box
10 input#Companies_b1280bf5a1b1-53e973a2a334__address.text-box
11 input#Companies_b1280bf5a1b1-53e973a2a334__street.text-box
12 input#Companies_b1280bf5a1b1-53e973a2a334__anotherthing.text-box
13 input#Companies_b1280bf5a1b1-53e973a2a334__another.text-box
14 input#Companies_b1280bf5a1b1-53e973a2a334__blabla.text-box
15 input#Companies_b1280bf5a1b1-53e973a2a334__loremipsum.text-box

....

Now I want all fields that end in "Name", for example, so, I tried this:

$('input[name$="Name"]', MyCompaniesInfo).val('Name');

but that doesn't work. How can I continue to select with jquery after MyCompaniesInfo = $('input[name^="Companies"]');?

4

1 回答 1

1

问题是当您使用第一个选择器时,MyCompaniesInfo = $('input[name^="Companies"]');您将在MyCompaniesInfo.

然后,如果要过滤必须使用的元素filter,如下所示。

$(MyCompaniesInfo).filter('input[name$="Name"]')

为什么下面的代码不起作用? $('input[name$="Name"]', MyCompaniesInfo)
因为它是相同的$(MyCompaniesInfo).find('input[name$="Name"]')并且 find 只通过 DOM 树进行搜索,就像它在 api 上描述的一样。

Given a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements.

在这里你可以看到一个演示

于 2012-12-11T00:27:33.233 回答