我有以下的代码,每一个元素发生变更我的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
});
});
//
你的例子中的'this'是什么? – jrummell 2012-03-06 19:42:35
“this”是javascript中的保留字,正确吗? – 2012-03-06 19:53:00
是的,但它的含义是上下文的。 – jrummell 2012-03-06 19:54:39