2012-11-27 43 views
2

我使用這個jquery https://github.com/tzuryby/jquery.hotkeys作爲熱鍵。jQuery熱鍵調用函數

請,我怎麼能稱之爲 「添加」 溫控功能:

<script type = "text/javascript" > 
    function add(int) { 
     if (window.XMLHttpRequest) { 
      xmlhttp = new XMLHttpRequest(); 
     } else { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
       document.getElementById("add").innerHTML = xmlhttp.responseText; 
      } 
     } 
     xmlhttp.open("GET", "method.php?a=" + int, true); 
     xmlhttp.send(); 
    } 
</script> 

例如,我需要調用add javasctipt,不會提醒:

<script src="jquery-1.4.2.js"></script> 
<script src="jquery.hotkeys.js"></script > 

<script> 
    $(document).bind('keydown', 'ctrl + k', function() { 
     alert('Teskt hotkey!'); 
     // I need processing javasctipt named "add", not alert 
    }); 
</script> 

回答

0

我建議你使用的jQuery 1.8.3 (can download here),因爲版本1.4.2已折舊。這是你應該如何調用該函數:

JAVASCRIPT:

<script src="jquery-1.4.2.js"></script> 
<script src="jquery.hotkeys.js"></script> 
<script type="text/javascript"> 
//here you are declaring the function 
function add(int) { 
    if (window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } else { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      document.getElementById("add").innerHTML = xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET", "method.php?a=" + int, true); 
    xmlhttp.send(); 
} 

$(document).ready(function(){ 
//when you use jquery, 
//it is good practice to wait until the document is ready. 
    $(document).bind('keydown', 'ctrl + k', function() { 
    //the add function requires an argument, so make sure to provide one. 
     add(1); 
    }); 
}); 
</script> 

希望這有助於讓我知道,如果你有任何問題。快樂的編碼!