2011-08-25 24 views

उत्तर

6

ऑनकीप फ़ंक्शन से पहले, एक चर घोषित करें। कुछ var _this = this और फिर कीप फ़ंक्शन में, this के बजाय _this का उपयोग करें।

तो अपने कोड की तरह कुछ दिखेगा:,

function FilterSelect(select, search) { 
    var _this = this; // <-- win 
    _this.select = select; 
    _this.search = search; 

    // Get the current list options 
    _this.options = this.select.options; 

    // Whenever the text of the search box changes, do this 
    _this.search.onkeyup = function() { 
     // Clear the list 
     while(this.select.options.length > 0) { 
      _this.select.remove(0); 
     } 
    } 
} 

ऐसा करने से आप:

var _this = this; 
// Whenever the text of the search box changes, do this 
this.search.onkeyup = function() { 
    // Clear the list 
    while(_this.select.options.length > 0) { 
     _this.select.remove(0); 
    } 
} 
3

आप एक चर जो onkeyup समारोह के बंद होने दायरे में आयोजित किया जाएगा बनाने की जरूरत सुनिश्चित करें कि onkeyup फ़ंक्शन के साथ कॉल करने के लिए उचित मूल्य का संदर्भ दिया जाएगा (आमतौर पर घटना के कारण वैश्विक/विंडो स्कोप)।

संपादित
वास्तव में, अगर आप सिर्फ select का उपयोग करने की जरूरत है, तो आप में सक्षम यह पहले से ही करने के लिए किया जाना चाहिए:

this.search.onkeyup = function() { 
    // Clear the list 
    while(this.select.options.length > 0) { 
     select.remove(0); 
    } 
} 
संबंधित मुद्दे