[英]using keypress event in javascript
我想使用按鍵刪除php會話變量。 所以我用這些代碼
<script type="text/javascript">
function textsizer(e){
var evtobj=window.event? event : e
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
var actualkey=String.fromCharCode(unicode)
if(actualkey=="x"){
location.href="biling.php?cmd="+actualkey;}
}
document.onkeypress=textsizer
</script>
<?php if(isset($_GET['cmd'])){
unset($_SESSION["bill_array"]);
header('location:biling.php');
}
?> }
但是問題是,當我在文本框中鍵入“ x”時,此代碼還會清除會話。 因此,我只想停止該操作並僅在我在文本框的外面按“ x”時清除會話
您可以檢查e.target
屬性以識別哪個元素觸發了keypress
事件。
//Does not work on IE
window.addEventListener('keypress', function(e){ console.log(e.target) }, false)
當e.target.tagName == 'BODY'
時采取措施是一個不錯的選擇。
有關Event
更多信息,請檢查:
我認為您可以這樣編輯現有功能:
function textsizer(e) {
var evtobj=window.event? event : e;
var element = evtobj.target ? evtobj.target : evtobj.srcElement;
if (element.tagName.toLowerCase() == "body") {
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
var actualkey=String.fromCharCode(unicode)
if(actualkey=="x"){
location.href="biling.php?cmd="+actualkey;
}
}
}
$(“ input [type ='text']”)。keypress(function(){
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.