2009-06-06 48 views
2

我想在某個特定表單中的某個元素髮生更改時觸發javascript函數。這可能使用jQuery嗎?JQuery表單更新偵聽器

有沒有人有一個例子如何做到這一點?

+0

一個`-1` 4年後已經提出這樣的問題?但爲什麼? – Fortega 2013-03-14 08:49:22

回答

2
$(function() { 
    $('form#id input[name=joe]').change(function() { alert('it changed!'); }); 
}); 
+0

謝謝......不知道那是簡單的......只是在窗體上調用change事件。 – Fortega 2009-06-06 23:35:18

2

如果你有這樣一個文本字段:

<input type='text' id='myInput'> 

你可以這樣做:

function executeOnChange() { 
    alert('new value = ' + $(this).val());  
} 
$(function() { // wait for DOM to be ready before trying to bind... 
    $('#myInput').change(executeOnChange); 
}); 

Here is an example of this in action

請注意,爲了使change事件觸發,您必須將注意力放在文本字段上。

如果要按名稱而不是ID選擇輸入,可以將$('#myInput')更改爲$('input[name=myName]')。有關選擇器的更多信息,請參見check out the documentation