使用 jQuery,选择下一个
根据标题,除了选择正确的框之外,我可以做所有事情。
页面布局基本上是几个链接(class =“class”),每个链接(class =“games”)旁边有一个选择框(选择框和链接之间有一些元素)。我试图仅更改紧邻被单击链接旁边的选择框的值。
* 编辑*
这是一个小提琴,其中包含我正在使用的 HTML 的更好示例: http ://jsfiddle.net/ynSeZ/9/
--- 我已经尝试了所有建议的答案,但没有一个有效。感谢迄今为止所做的一切努力! ---
这就是我到目前为止所拥有的..当我删除最接近的()部分时,它可以工作..但它改变了每个选择框的值,而不仅仅是最接近链接的方式。
提前致谢, 菲尔
Based on the title, I am able to do everything except select the correct box.
The page layout is basically several links (class="class") with a select box next (there are some elements in between the select box and the link) to each link (class="games"). I am trying to change the value ONLY of the select box immediately next to the link being clicked.
* EDIT *
here's a fiddle with a better example of the HTML I'm using: http://jsfiddle.net/ynSeZ/9/
--- i have tried all of the answers suggested and none have worked. thanks for all the effort so far ! ---
this is what i have so far.. when i remove the closest() part, it works.. but it changes the value of every select box instead of only the way closest to the link.
thanks in advance,
phil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是你的答案。使用
siblings
而不是closest
:小提琴:http://jsfiddle.net/maniator/ynSeZ/3/
现在我想了一下,上面的答案可能不正确。您必须实现一些东西才能获取最接近的
select
元素。上面的代码的作用是获取页面上的第一个选择元素,不一定是最接近a
标记的UPDATE
这是基于您最新的小提琴的答案:
Fiddle : http://jsfiddle.net/maniator/ynSeZ/12/
Here is your answer. Use
siblings
instead ofclosest
:Fiddle: http://jsfiddle.net/maniator/ynSeZ/3/
Now that I think about it, the above answer may be incorrect. You have to implement something to get the closest
select
element. What the above code does is get the first select element on the page, not necessarily the one closest to thea
tagUPDATE
Here is an answer based off your newest fiddle:
Fiddle: http://jsfiddle.net/maniator/ynSeZ/12/
您应该更改 HTML,以便被点击的链接只有一个“同级”选择元素。像这样:
然后在 Javascript 中执行此操作以获得正确的选择元素:
其中
this
是被单击的链接。closest
函数有点误导。它找到与给定选择器匹配的元素的最近祖先。You should change your HTML so that the link that gets clicked has only one "sibling" select element. Like this:
Then do this in your Javascript to get the right select element:
where
this
is the link that was clicked.The
closest
function is kind of misleading. It finds the closest ancestor of the element that matches the given selector.是时候来一个好的老式
while
循环了:jsFiddle: http://jsfiddle。 net/cM4B5/3/
编辑:jsFiddle 中的轻微拼写错误导致第二个链接无法工作。已更新。
Time for a good, old-fashioned
while
loop:jsFiddle: http://jsfiddle.net/cM4B5/3/
Edit: Slight typo in the jsFiddle prevented the second link from working. Updated.
也许是这样的?
已更新
这将抓取同一行中的选择框并将值设置为单击链接的 team_id
Maybe something like this?
UPDATED
This will grab the select box in the same row and set the value as the team_id of the clicked link