jQuery选择器按名称获取表单 [英] jQuery selector to get form by name
问题描述
我有以下HTML:
<form name="frmSave">...</form>
只知道,我不能修改HTML以添加id
或其他内容.
Just to know, I am not able to modify the HTML in order to add an id
or something else.
这就是我试图通过其名称获取form
元素的原因:
This is what I tried to get the form
element by it's name:
var frm = $('form[name="frmSave"]');
console.log(frm);
(但是我相信上面的代码将尝试在form
内获取一个具有frmSave
名称的子元素,这不是我所需要的.)
(but I believe the above code will try to get a children element with the frmSave
name inside the form
which is not what I need).
我该如何实现?是否可以仅通过name
并使用选择器来获得form
?
How can I achieve this, is it possible to get the form
by just the name
and using a selector?
我在原始代码中做错了方法(在选择器中使用了空格,也就是组合器"),而不是在这里的代码段中,因此jQuery试图获取具有所需名称的所有子元素,而我问自己是什么问题,因为什么也没回来.
I was doing the wrong way in my original code (I was using a space aka "combinator" in the selector) and not in the code snippet from here so jQuery was trying to get any children element with the name needed and I was asking myself what was wrong since nothing was returned.
被接受的答案对此有一个更好的解释,所以一点空间就能改变一切-下次我会正常睡觉:-)
The accepted answer gives a better explanation about this so a little space could change everything - I will sleep properly next time :-)
推荐答案
$('form[name="frmSave"]')
是正确的.您提到您以为这样做会使表格中所有名称为frmsave
的孩子都得到;仅当表单和选择器之间存在空格或其他组合符时,才会发生这种情况,例如:$('form [name="frmSave"]');
$('form[name="frmSave"]')
is correct. You mentioned you thought this would get all children with the name frmsave
inside the form; this would only happen if there was a space or other combinator between the form and the selector, eg: $('form [name="frmSave"]');
$('form[name="frmSave"]')
的字面意思是查找名称为frmSave
的所有形式,因为不涉及组合器.
$('form[name="frmSave"]')
literally means find all forms with the name frmSave
, because there is no combinator involved.
这篇关于jQuery选择器按名称获取表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!