[英]Access multiple elements with ID from within a node, but also with specific class
I need to retrieve all elements who's ID ends with "_inputError" from within a specific element , AND also , I need to retrieve only the inputError that has class 'b-block' set.. How would I do this using javascript/jquery?我需要从特定元素中检索ID 以“_inputError”结尾的所有元素,而且,我只需要检索设置了class 'b-block'的 inputError。我将如何使用 javascript/jquery 执行此操作?
I know I can get my form node with: var formNode = $("#frm1");
我知道我可以得到我的表单节点:
var formNode = $("#frm1");
I know I can get ALL elements with ID ending, but this will get them all, which isn't quite what I need: var inputErrors = $("[id$=_inputError]");
我知道我可以获取所有以 ID 结尾的元素,但这将获取所有元素,这不是我所需要的:
var inputErrors = $("[id$=_inputError]");
. .
How do I mix'em all together as to retrieve only the input errors with 'd-block' class from within a specified form node?我如何将它们混合在一起,以便从指定的表单节点中仅检索带有“d-block”class 的输入错误?
Cheers!干杯!
UPDATE: This seems to work for me:更新:这似乎对我有用:
var inputErrors = $("[id$=_inputError].d-block");
console.warn(inputErrors[0]);
It'll retrieve all inputError with class.d-block.它将使用 class.d-block 检索所有 inputError。 However, I've yet to find how to do this check ONLY from a selected form element id, instead of the hole page.
但是,我还没有找到如何仅从选定的表单元素 ID 而不是漏洞页面来执行此检查。 (I can have multiple forms on a single page!)
(我可以在一个页面上有多个 forms!)
I need to retrieve all elements who's ID ends with "_inputError" from within a specific element , AND also , I need to retrieve only the inputError that has class 'b-block' set.. How would I do this using javascript/jquery?我需要从特定元素中检索ID 以“_inputError”结尾的所有元素,并且,我只需要检索设置了class 'b-block'的 inputError .. 我将如何使用 javascript/jquery 执行此操作?
I know I can get my form node with: var formNode = $("#frm1");
我知道我可以通过以下方式获取表单节点:
var formNode = $("#frm1");
I know I can get ALL elements with ID ending, but this will get them all, which isn't quite what I need: var inputErrors = $("[id$=_inputError]");
我知道我可以得到所有以 ID 结尾的元素,但这会得到所有元素,这不是我所需要的:
var inputErrors = $("[id$=_inputError]");
. .
How do I mix'em all together as to retrieve only the input errors with 'd-block' class from within a specified form node?如何将它们混合在一起,以便从指定的表单节点中仅检索带有 'd-block' class 的输入错误?
Cheers!干杯!
UPDATE: This seems to work for me:更新:这似乎对我有用:
var inputErrors = $("[id$=_inputError].d-block");
console.warn(inputErrors[0]);
It'll retrieve all inputError with class.d-block.它将使用 class.d-block 检索所有 inputError。 However, I've yet to find how to do this check ONLY from a selected form element id, instead of the hole page.
但是,我还没有找到如何仅从选定的表单元素 id 而不是孔页进行此检查。 (I can have multiple forms on a single page!)
(我可以在一个页面上有多个 forms!)
To help others if it can, here's the final solution and why I needed this:为了尽可能帮助他人,这是最终解决方案以及我需要它的原因:
Essentially, I get a response back from an ajax post like so:本质上,我从 ajax 帖子中得到回复,如下所示:
{
type: 2,
formid: 'frmProfile',
fields: {
username: "Invalid username!",
email: "Invalid email!"
}
}
I loop my response.fields to show the individual errors under each failed inputs.我循环我的 response.fields 以显示每个失败输入下的个别错误。
I then needed to find a way set focus on the first failed input, Because my fields are NOT 0-indexed based, I could not simply set the focus on the first field in the array!然后我需要找到一种方法将焦点设置在第一个失败的输入上,因为我的字段不是基于 0 索引的,所以我不能简单地将焦点设置在数组中的第一个字段上! They are 'randomly' looped through!
它们是“随机”循环的!
Therefor, I decided to get all input errors that we're showing (id=*_inputError and with class d-block).因此,我决定获取我们显示的所有输入错误(id=*_inputError 和 class d-block)。 The first one in the returned array is in fact the first input error, So: I use its ID to determine the corresponding input name and set focus to it:
返回数组中的第一个实际上是第一个输入错误,所以:我使用它的ID来确定对应的输入名称并将焦点设置到它上面:
var inputErrors = $("#" + response.formid + " [id$=_inputError].d-block");
var firstInputError = inputErrors[0].id.replace(/_inputError$/, ''); // get just the name of the failed input! Its name/id should be the same as <name>_inputError!
document.getElementById(firstInputError).focus();
This seems to do the trick for me;) Hopefully, this can serve ideas to others.这似乎对我有用;)希望这可以为其他人提供想法。
Thanks for your help folks!感谢您的帮助! P
P
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.