将 Jquery 选择器的名称设置为变量中的字符串

发布于 2024-09-26 23:16:13 字数 692 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

银河中√捞星星 2024-10-03 23:16:13

focus 事件不会冒泡,请参阅 http:// www.quirksmode.org/dom/events/blurfocus.html#t06

其他几个问题:

  1. 分配给事件处理程序内部的 formName 变量与第一个变量不同行,因为您重新声明了它,所以它仅存在于该事件函数的范围内。
  2. parentsUntil 将返回所有祖先直到 < code>form 元素,而不是元素本身,这可能是您想要的。

您的代码脱离了上下文,因此很难理解如何使用 formName 和计时器变量以及应在何处声明它们,但这应该适用于事件:

$('form :input').focus(function() {
    formName = $(this).parents('form').attr('name');
});

:input 是一个 jQuery 选择器,它匹配所有 类型,以及

The focus event will not bubble, see http://www.quirksmode.org/dom/events/blurfocus.html#t06

Couple of other issues:

  1. The 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.
  2. parentsUntil will return all ancestors until the form 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:

$('form :input').focus(function() {
    formName = $(this).parents('form').attr('name');
});

:input is a jQuery selector that matches all <input> types, along with <textarea> and <select>

雨后彩虹 2024-10-03 23:16:13

focus 事件会冒泡,所以你可以只使用:

$('form').focus(function() {
  formName = this.name;
});

但最好在提交时存储你的计时器,例如:

$('form').submit(function() {
  formName = this.name;
});

另外不要在内部使用 var事件处理程序,因为这将创建一个新的局部变量,设置在其上面声明的 formName 变量。

The focus event will bubble, so you can just use:

$('form').focus(function() {
  formName = this.name;
});

But it'd be better to store your timer when it's submitted, for example:

$('form').submit(function() {
  formName = this.name;
});

Also don't use var inside the event handler, as this will create a new, local variable, not set your formName variable declared above it.

╭⌒浅淡时光〆 2024-10-03 23:16:13
var formName = 0;
$('input').parentsUntil('form', this).focus(function() {var formName = this.name;})

这是行不通的。您试图分配给第一行中定义的 formName 变量,但您正在第二行中重新定义该变量。您实际上正在处理两个不同的变量,一个在函数范围内,一个在全局范围内。

解决方案:

var formName = 0;
$('input').parentsUntil('form', this).focus(function() {formName = this.name;})
//                                                      --- no var here
var formName = 0;
$('input').parentsUntil('form', this).focus(function() {var formName = this.name;})

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:

var formName = 0;
$('input').parentsUntil('form', this).focus(function() {formName = this.name;})
//                                                      --- no var here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文