如何使用 jQuery 选择相邻行的输入子项?
我希望有人可以帮助我了解 jQuery 选择器的语法。我就是无法让它发挥作用! 我正在尝试选择与包含具有类跨度的 div 的表行相关的输入字段。 HTML 如下:
<tbody>
<tr><th scope="col"><label for="username">Username</label><br /><?php echo form_error('username');?></th></tr>
<tr><td><input type="text" id="username" name="username" value="<?php echo set_value('username');?>"/></td></tr>
<tr><th scope="col"><label for="password">Password</label><br /><?php echo form_error('password');?></th></tr>
<tr><td><input type="password" id="password" name="password" /></td></tr>
</tbody>
如果任一字段出现错误,将创建一个 div (echo form_error),该 div 具有“form_error”类并包含描述错误的文本。
如果出现错误,我想选择文本输入,以便我可以通过更改 CSS 来突出显示,例如将边框的颜色更改为红色。
这是我尝试过但不起作用的 jQuery:
$(document).ready(function() {
$("tr:has(.form_error)").next().children("td:input").css("border-color", "#C21312");
});
有什么建议吗?
I'm hoping someone can help me with the syntax for a jQuery selector. I just can't get it to work!
I'm trying to select the input field that relates to the table row that contains a div with class span. Here's the HTML:
<tbody>
<tr><th scope="col"><label for="username">Username</label><br /><?php echo form_error('username');?></th></tr>
<tr><td><input type="text" id="username" name="username" value="<?php echo set_value('username');?>"/></td></tr>
<tr><th scope="col"><label for="password">Password</label><br /><?php echo form_error('password');?></th></tr>
<tr><td><input type="password" id="password" name="password" /></td></tr>
</tbody>
If there is an error with either field a div will be created (echo form_error) which has a class of "form_error" and has text describing the error.
I want to select the text input if there is an error so that I can highlight by changing the CSS, e.g. changing the colour of the border to red.
This is the jQuery I tried but isn't working:
$(document).ready(function() {
$("tr:has(.form_error)").next().children("td:input").css("border-color", "#C21312");
});
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有两个重大错误。请允许我分解一下您的代码:
正如 Sarfraz 指出的那样,您需要在“td:input”选择器中留出一个空格 - td 元素永远不是表单元素,显然您正在寻找一个输入子无论如何,td元素。
但这只会暴露第二个错误:您不是在寻找该行的直接子代,而是在寻找孙子 - 但是
children()
将仅搜索上下文元素的直接后代。您真正想要使用的是find()
< /a>...请注意,出于您的目的,您实际上并不需要使用
:input
选择器 -input
也可以正常工作,因为在您给出的示例中,它实际上是一个元素您正在寻找...但如果您还计划使用其他表单控件,:input 将为您提供更多的灵活性。
You have two major errors. Allow me to break out your code:
As Sarfraz pointed out, you need a space in the "td:input" selector - td elements are never form elements, and obviously you're looking for an input child of the td element anyway.
But that only exposes the second error: you're not looking for a direct child of the row, you're looking for a grandchild - yet
children()
will search only the immediate descendants of the context elements. What you really want to use isfind()
...Note that for your purposes, you don't really need to use the
:input
selector -input
would work just fine as well, since in the example you gave, it's actually an<input>
element you're looking for... But if you plan on using other form controls as well, :input will give you slightly more flexibility.