2012-03-06 73 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。瞧!