jQuery 选择器按名称获取表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13748669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery selector to get form by name
提问by Oscar Jara
I have the following HTML:
我有以下 HTML:
<form name="frmSave">...</form>
Just to know, I am not ableto modify the HTML in order to add an id
or something else.
只是要知道,我无法修改 HTML 以添加id
或其他内容。
This is what I tried to get the form
element by it's name:
这就是我试图form
通过名称获取元素的方法:
var frm = $('form[name="frmSave"]');
console.log(frm);
(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).
(但我相信上面的代码将尝试获取一个frmSave
名称在里面的子元素,form
这不是我需要的)。
How can I achieve this, is it possible to get the form
by just the name
and using a selector?
我怎样才能做到这一点,是否可以form
通过name
使用选择器来获得?
Update:
更新:
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.
我在我的原始代码中做错了(我在选择器中使用了一个空格,也就是“组合器”)而不是在这里的代码片段中,所以 jQuery 试图获取任何具有所需名称的子元素,我在问自己出了什么问题,因为什么都没有返回。
The accepted answer gives a better explanation about this so a little space could change everything - I will sleep properly next time :-)
接受的答案对此给出了更好的解释,因此一点空间可以改变一切-下次我会好好睡觉:-)
回答by Christian
$('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"]');
$('form[name="frmSave"]')
literally means find all forms with the name frmSave
, because there is no combinator involved.
$('form[name="frmSave"]')
字面意思是查找所有带有 name 的形式frmSave
,因为不涉及组合器。
回答by Arun Pratap Singh
// this will give all the forms on the page.
$('form')
// If you know the name of form then.
$('form[name="myFormName"]')
// If you don't know know the name but the position (starts with 0)
$('form:eq(1)') // 2nd form will be fetched.
回答by Niet the Dark Absol
You have no combinator (space, >
, +
...) so no children will get involved, ever.
你有没有组合子(空间>
,+
...),所以没有孩子会涉足,直到永远。
However, you could avoid the need for jQuery by using an ID
and getElementById
, or you could use the old getElementsByName("frmSave")[0]
or the even older document.forms['frmSave']
. jQuery is unnecessary here.
但是,您可以通过使用ID
and来避免对 jQuery 的需求getElementById
,或者您可以使用旧的getElementsByName("frmSave")[0]
或更旧的document.forms['frmSave']
. 这里不需要 jQuery。
回答by Mike Irving
For detecting if the form is present, I'm using
为了检测表单是否存在,我正在使用
if($('form[name="frmSave"]').length > 0) {
//do something
}