2016-05-26 39 views
1

我試圖從在線onkeyup事件移動和改變了我的輸入來自的JavaScript獲得情況下,本

<input name="some1" id="some1" onkeyup="ajax_autocomplete('some1',this.value,event);"> 

<input name="some1" id="some1" class="autoc"> 

和頁面加載

var acinputs = document.querySelectorAll('.autoc'); 
for(var i=0; field=acinputs[i]; i++) 
{ 
field.onkeyup = function() {update.call(this);} 
    function update() 
    { 
    var text = this.value; 
    // How to get keycode now from event? 
    // Before: var kc=event.which;if(kc==undefined){kc=event.keyCode} 
    } 
} 

我的問題後,是當我得到event數據內聯像keycodeevent之前,但我不知道如何從事件現在得到它? this.value給我在input中輸入的文字,但是this.event不起作用!我知道this.event不是從this獲得活動的正確方法!

我是不是使用jQuery。

+1

出於好奇,爲什麼你不能(或不願意)使用jQuery? – Krease

+2

jquery是偉大的,並做所有的事情http://i.stack.imgur.com/ssRUr.gif – jwatts1980

+0

可能重複[爲什麼FF說window.event是未定義的? (調用函數添加事件監聽器)](http://stackoverflow.com/questions/9813445/why-ff-says-that-window-event-is-undefined-call-function-with-added-event-list) –

回答

5

添加事件作爲參數

在我的例子event被稱爲ev

field.onkeyup = function(ev) {update.call(this, ev);} 

function update(th, ev) 
{ 
    var text = th.value; 

    var kc=ev.which; 
    if(kc==undefined) { 
     kc=ev.keyCode 
    } 
} 
+1

爲什麼不直接分配'update '到'onkeyup'並避免額外的方法調用? – mdickin

+2

@mdickin這只是對OP的回覆。我沒有優化作者代碼。 – R3tep

+0

@mdickin你是對的我刪除了調用方法並直接分配。仍在試驗,我從互聯網教程中複製了該代碼。再次感謝您的幫助。 –

相關問題