2010-11-24 19 views
5

當前元素我有一個元素作爲其活動功能PARAM

<input type="text"> 

在此,有一個事件

onChange="myfunction(param)". 

「參數」是輸入本身的內容。我如何處理,當我開始改變(如此完成領域的變化)時,在這個參數中是這個領域的實際價值?

是否有可能做這樣的事情:

onChange="myfunction(document.getElementById('this_id'))" 
+0

你可以用`平變化= 「myfunction.call(本)」'從這裏:https://stackoverflow.com/a/12812974/1720476其中「this」將指向當前對象,然後您可以根據需要使用`this.` heyword在被調用的函數中獲得參數.. – 2017-10-08 14:49:13

回答

18

你可以通過thismyFunction這將是input

<input type="text" onChange="myfunction(this)" /> 

然後myFunction看起來是這樣的:

function myFunction(obj) 
{ 
    var value = obj.value; // the value of the textbox 
} 
+0

OK thanx;)我忘記了「this」存在; ) 非常感謝你 – 2010-11-24 15:00:07

2

內聯事件句柄r,this將引用DOM元素。

因此,您可以編寫onchange="myfunction(this)"將DOM元素本身傳遞給該函數。

2

要獲得.value內聯,它應該是這樣的:

<input type="text" onchange="myfunction(this.value)" />