2012-03-06 70 views
5

我有以下的代码,每一个元素发生变更我的web的表单中时间的工作原理:如何使用jQuery和“本”来捕捉变化的表单元素值

<!-- 

jQuery.support.cors = true; // needed for ajax to work in certain older browsers and versions 

$(document).ready(function(){ 

    $(this).change(function(){ 
     alert('form element changed!'); 
    }); 

}); // end .ready() 

//--> 

我一直在挣扎是如何捕捉表单字段元素ID更改值在触发更改事件。

任何人都可以帮我解决这个问题吗?

谢谢!

** JavaScript文件**

// Sarfraz 
$(this).change(function(){ 
    var id, name, value; 
    id = this.id, name = this.name, value = this.value; 
    alert('id=' + id); // this returns 'undefined' 
    alert('name=' + name); // this returns 'undefined' 
    alert('value=' + value); // this returns 'undefined' 
}); 
// 

// rjz 
$(this).change(function(){ 
    var $this = $(this), 
    id = $this.attr('id'), 
    name = $this.attr('name'), 
    value = $this.val(); 

    alert(id); // this returns 'undefined' 
    alert(name); // this returns 'undefined' 
    alert(value); // this returns blank 
}); 

// Jamie 
$(this).change(function(){ 
    var id = $(this).attr('id'); 
    var name = $(this).attr('name'); 
    var value = $(this).attr('value'); 

    alert('id=' + id); // this returns 'undefined' 
    alert('name=' + name); // this returns 'undefined' 
    alert('value=' + value); // this returns 'undefined' 
}); 
// 

//James Allardice 
$(this).change(function(e) { 
    var elem = e.target; 
    alert('elem=' + elem); // this returns 'objectHTMLTextAreaElement' 
}); 
// 

// Surreal Dreams 
$("#my-form input").change(function(){ 
    alert('form element changed!'); 
    var value = $(this).val(); 
    var id = $(this).attr("id"); 
    var name = $(this).attr("name"); 

    alert(id); // nothing happens 
    alert(name); // nothing happens 
    alert(value); // nothing happens 
}); 
// 

//Jamie - Second Try 
$('.jamie2').each(function() { 
    $(this).change(function(){ 
     var id = $(this).attr('id'); 
     alert(id); // nothing happens 
    }); 
}); 
// 
+1

你的例子中的'this'是什么? – jrummell 2012-03-06 19:42:35

+0

“this”是javascript中的保留字,正确吗? – 2012-03-06 19:53:00

+0

是的,但它的含义是上下文的。 – jrummell 2012-03-06 19:54:39

回答

4

据我的理解,是指form元素,你想要得到的一个参考触发事件的form元素的后代。

如果这是正确的,你可以使用事件对象的target属性:

$(this).change(function(e) { 
    var elem = e.target; 
}); 

这里有一个working example

在上面的代码中,elem将引用触发事件的元素。然后,您可以访问性质的元素,如id

$(this).change(function(e) { 
    var elemId = e.target.id; 
}); 
+0

当我尝试了您的示例时,我得到了'objectHTMLTextAreaElement'。看到我附加到我的问题的代码。 – 2012-03-06 20:02:20

+0

@ Dr.DOT - 试试'alert(e.target.id);'。 'e.target'为你提供实际的元素。然后您可以访问它的属性。我用另一个例子更新了我的答案。 – 2012-03-06 20:03:27

+0

YAHOO @JamesAllardice。你到目前为止我最近。我刚刚看到了ID的名称。我将与你的样品一起玩。我会回报。你是个明星。所有其他人都在这里插话。感谢大家!!! – 2012-03-06 20:24:50

5

我认为你可能有一个问题,从一开始:

$("#myform input").change(function(){ 
    alert('form element changed!'); 
    var value = $(this).val(); 
    var id = $(this).attr("id"); 
    var name = $(this).attr("name"); 
}); 

你不想开始与$(本),你需要选择你想要监控的输入。然后你可以在change()函数中使用$(this)。

James Allardice指出,您可能指的是带有$(this)的窗体,并且change()事件会捕获窗体中的所有更改。我建议你更具体地针对更改后的元素,以便不捕获不需要或不需要的元素的更改事件,这可以消除意外的行为。您可以使用像:input这样的类或表单选择器来定位它们。

+0

我会同意这一点,直到DOT博士为您提供更多信息想要将变化绑定到“this”而不是特定输入。 ;-)我还没有想到'$(this).change()'的好用例,但这并不意味着没有一个用例。 – 2012-03-06 19:43:06

+0

@GregPettit - 由于'this'引用了一个'form'元素,因此可以将一个'change'事件处理函数绑定到它,并且它将捕获由它的任何后代触发的变化事件(因为它们会冒泡到'form' )。就像在选择器参数中使用'on'一样。看到我的答案。 – 2012-03-06 19:44:01

+0

如果DOT博士提供了一些解释,我一定会做出更新。在这里扔一个评论博士点,所以我收到一张纸条来检查它。 – 2012-03-06 19:45:53

1

你会做这样的事情你改变里面以下

var ELEMEMT = $('#IDOFELEMENT').val(); 
+0

非常感谢你帮助我。通过James Allardice的'e.target.id'答案,我能够得到我所阐述的内容。我非常感谢你的协助。最好的祝福。 – 2012-03-06 20:32:17

1

您可以直接访问属性如下:

$(this).change(function(){ 
    var id = this.id, 
    name = this.name, 
    value = this.value; 
}); 

另外,jQuery提供辅助函数来检索第一这些属性元素在jQuery集合中:

$(this).change(function(){ 
    var $this = $(this), 
    id = $this.attr('id'), 
    name = $this.attr('name'), 
    value = $this.val(); 
}); 
+0

你的第一个例子与Sarfraz相同。你的第二个例子也是我测试过的。两个样本都返回未定义。在我的问题中看到我的JS代码。 – 2012-03-06 19:56:17

+0

非常感谢您帮助我。通过James Allardice的'e.target.id'答案,我能够得到我所阐述的内容。我非常感谢你的协助。最好的祝福。 – 2012-03-06 20:31:44

1

尝试使用jQuery 1.7

$(document).on('change','#myID',function(){ 
    alert('Id:'+this.id+'\nName:'+this.name+'\nValue:'+this.value); 
});​ 

DEMO

+0

我认为#myID是参考我的

标记ID。正确?如果是这样,当我厌倦你的建议时没有任何回报。 – 2012-03-06 20:05:28

+0

非常感谢您帮助我。通过James Allardice的'e.target.id'答案,我能够得到我所阐述的内容。我非常感谢你的协助。最好的祝福。 – 2012-03-06 20:30:44

1

方法如下:this

$(this).change(function(){ 
    var id, name, value; 
    id = this.id; name = this.name; value = this.value; 
}); 
+0

谢谢@Sarfraz。我几天前尝试过你的版本,但又给了它一次。看到我的问题附加我的代码。所有这三个元素都返回“未定义”。 – 2012-03-06 19:51:51

+0

@ Dr.DOT:您还需要澄清'this'在您的代码中所指的是什么,html元素以及它到达该代码的方式。 – Sarfraz 2012-03-06 19:54:23

+0

“this”是javascript中的保留字,正确吗? – 2012-03-06 20:03:06

2

为了使用$(本),你必须有一个预定义的JavaScript对象。这就是为什么它叫

所以,你需要做这样的事情:

$('.class_name').each(function() { 
    $(this).change(function(){ 
     var id = $(this).attr('id'); 
    }); 
}); 

$('.class_name').click(function() { 
    $(this).change(function(){ 
     var id = $(this).attr('id'); 
    }); 
}); 

总之,你需要选择一个元素,并创建一个对象,你可以使用$(本)前。

+0

@Jaimie。每个示例代码都返回'undefined'。看到我附加到我的问题的代码。 – 2012-03-06 19:59:08

+0

我编辑了我的问题 – hohner 2012-03-06 20:01:35

+0

谢谢@Jaimie ...但仍然没有运气。看到我的代码我附加到我的问题。 – 2012-03-06 20:18:16

0

好了,我来晚了党除了在评论形式,而只是为后人:这是我的贺岁片梦想的方式扩展:

$("#myform").on('change', input, (function(){ 
    alert('form element changed!'); 
    var $this = $(this); 
    var value = $this.val(); 
    var id = $this.attr("id"); 
    var name = $this.attr("name"); 
}); 

#myform监听里面输入的变化。我也缓存了jQuery包装的this。瞧!