2013-06-12 3 views
5

इस कोड फ़ायरफ़ॉक्स (V21.0) में मेरे लिए काम नहीं है, लेकिन आईई (V9, V10) और क्रोम (वी 27.0.1453.110 मी)ReferenceError: घटना mozila में निर्धारित नहीं है फ़ायरफ़ॉक्स

विधि कैलोरी में काम करता है :

<input type="text" id="txt1" class="search-bar-input" 
         onkeyup="handleKeyPress('btn1');"> 

विधि defenition:

function handleKeyPress(searchButtonId) { 
    if (event.keyCode === 13) { 
    alert(event.KeyCode); 
    } 
} 

त्रुटि संदेश:

ReferenceError: event is not defined 
if (event.keyCode === 13) { 

किसी को भी इस मुद्दे को हल करने का कोई विचार है?

उत्तर

10

उपयोग

<input type="text" id="txt1" class="search-bar-input" onkeyup="handleKeyPress(event, 'btn1');"> 

फिर

function handleKeyPress(event) { 
    event = event || window.event //For IE 
    if (event.keyCode === 13) { 
     alert(event.keyCode); 
    } 
} 

डेमो: Fiddle

+0

आईई 9 –

+1

में काम नहीं कर @Arun_C_C ऐसा इसलिए है क्योंकि IE में onkeyup onkeypress कोशिश के बजाय कुंजी बलों पृष्ठ पुनः लोड दर्ज http://jsfiddle.net/arunpjohny/T9x9L/2/ –

+0

फ़ायरफ़ॉक्स 50.1.0 में काम नहीं कर रहा है –

2

आप btn1 पैरामीटर को data-* विशेषता के रूप में स्टोर कर सकते हैं और ईवेंट हैंडलर (इनलाइन के बजाय) असाइन करने के लिए अविभाज्य जावास्क्रिप्ट का उपयोग कर सकते हैं।

आपकी लाइन alert(event.KeyCode); गलत है, KeyPress में k लोअरकेस होना चाहिए।

Fiddle

एचटीएमएल

<input type="text" id="txt1" class="search-bar-input" data-searchButtonId="btn1" /> 

जावास्क्रिप्ट:

function handleKeyPress(event) { 
    if (event.keyCode === 13) { 
     console.log(event.keyCode + this.getAttribute("data-searchButtonId")); 
    } 
} 

window.onload = function(){ 
    document.getElementById("txt1").onkeyup = handleKeyPress; 
} 
संबंधित मुद्दे