将 Jquery 选择器的名称设置为变量中的字符串
我对 Jquery 很菜鸟,但我试图根据表单元素的名称设置一个变量。
即基于此示例的变量 =“someForm”:
<form id="hello" name="someForm" >
First name: <input type="text" name="firstname" />
Last name: <input type="text" name="lastname" />
Email: <input type="text" name="email" />
<input type="submit" value="Subscribe" />
</form>
我正在使用以下代码,但它似乎不起作用:
var formName = 0;
$('input').parentsUntil('form', this).focus(function() {var formName = this.name;})
if (stTime == 0) { // This ensures someone cannot start a form multiple times
var stTime = new Date();
stDelay = stTime - loTime;
alert(formName);
}
提前致谢!
I'm quite a noob at Jquery but I'm trying to set a variable based on the name of a form element.
I.e. variable = "someForm" based on this example:
<form id="hello" name="someForm" >
First name: <input type="text" name="firstname" />
Last name: <input type="text" name="lastname" />
Email: <input type="text" name="email" />
<input type="submit" value="Subscribe" />
</form>
I'm using the following code which doesn't seem to work:
var formName = 0;
$('input').parentsUntil('form', this).focus(function() {var formName = this.name;})
if (stTime == 0) { // This ensures someone cannot start a form multiple times
var stTime = new Date();
stDelay = stTime - loTime;
alert(formName);
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
focus
事件不会冒泡,请参阅 http:// www.quirksmode.org/dom/events/blurfocus.html#t06其他几个问题:
formName
变量与第一个变量不同行,因为您重新声明了它,所以它仅存在于该事件函数的范围内。parentsUntil
将返回所有祖先直到 < code>form 元素,而不是元素本身,这可能是您想要的。您的代码脱离了上下文,因此很难理解如何使用
formName
和计时器变量以及应在何处声明它们,但这应该适用于事件::input
是一个 jQuery 选择器,它匹配所有类型,以及
The
focus
event will not bubble, see http://www.quirksmode.org/dom/events/blurfocus.html#t06Couple of other issues:
formName
variable being assigned to inside the event handler isn't the same variable as the first line since you're re-declared it, it then exists only inside the scope of that event function.parentsUntil
will return all ancestors until theform
element, not the element itself which is presumably what you want.Your code is out of context so it's difficult to understand how
formName
and the timer variables should be used and where they should be declared, but this should work for the event::input
is a jQuery selector that matches all<input>
types, along with<textarea>
and<select>
focus
事件会冒泡,所以你可以只使用:但最好在提交时存储你的计时器,例如:
另外不要在内部使用
var
事件处理程序,因为这将创建一个新的局部变量,不设置在其上面声明的formName
变量。The
focus
event will bubble, so you can just use:But it'd be better to store your timer when it's submitted, for example:
Also don't use
var
inside the event handler, as this will create a new, local variable, not set yourformName
variable declared above it.这是行不通的。您试图分配给第一行中定义的 formName 变量,但您正在第二行中重新定义该变量。您实际上正在处理两个不同的变量,一个在函数范围内,一个在全局范围内。
解决方案:
This can't work. You are trying to assign to the formName variable defined in the first line, but you're redefining the variable in the second line. You are actually dealing with two different variables, one in the function scope and one in global scope.
Solution: