日期:2014-05-16  浏览次数:20317 次

js 触发键盘事件
请问大家   js可以触发键盘事件吗
例如   select中有多条数据     写一js方法     相当于按了键盘的ctrl
让其可以多选
            不知道行不行,谢谢大家哦

------解决方案--------------------
哈,好险!没有食言!

代码如下,单击加选,再次单击选中项则取消该项!
缺点:就是有点儿闪!
期待:无刷新版!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<html>
<head>
<title> New Document </title>
<meta name= "Generator " content= "EditPlus ">
<meta name= "Author " content= " ">
<meta name= "Keywords " content= " ">
<meta name= "Description " content= " ">
<script language= "JavaScript ">
<!--

//-->
</script>
</head>
<body>
<select id= "oselect " name= "cars " size= "10 " multiple>
<option value= "BMW "> 宝马 </option>
<option value= "Porsche "> 保时捷 </option>
<option value= "Benz "> 奔驰 </option>
<option value= "Santana "> 桑塔纳 </option>
<option value= "LK "> 林肯 </option>
<option value= "? "> 标志 </option>
<option value= "Bus "> 大公共 </option>
</select>
<input type= "button " id= "btnShowSelected " value= "查看选中项索引 ">
<span id= "spaOutput "> </span>
</body>
<script language= "JavaScript ">
<!--

String.prototype.trimEnd = function(trimString) {
var re = new RegExp(trimString+ "*$ ", "g ");
return this.replace(re, " ");
};

Array.prototype.indexOf = function(itemValue) {
var nIndex = -1;
for (var i=0; i <this.length; i++)
{
if (this[i] == itemValue)
nIndex = i;
}
return nIndex;
};


var oSpan = document.getElementById( "spaOutput ");
var oSele = document.getElementById( "oSelect ");
var oBtn = document.getElementById( "btnShowSelected ");

oBtn.onclick = function() {
alert(getSelectedIndexes(oSele));
};

function getSelectedIndexes(oSele)
{
var sSelectedIndexes = " ";
var separator = ", ";

for (var i=0; i <oSele.options.length; i++)
{
if (oSele.options[i].selected)
sSelectedIndexes += i.toString() + separator;
}

if (sSelectedIndexes == " ")
return new Array(0);
else
return sSelectedIndexes.trimEnd(separator).split(separator);
}

oSele.onclick = function() {
this.selectedIndexes = getSelectedIndexes(this);

// Debug
oSpan.innerHTML += " <br /> " + "onclick " + this.selectedIndexes;

if (this.selectedIndexes.length == 1)
{
var nSelectedIndex = parseInt(this.selectedIndexes[0]);
if (!isNaN(nSelectedIndex))
this.options[nSelectedIndex].selected = false;
}
};
oSele.onchange = function() {
// Debug
oSpan.innerHTML += " <br /> " + "onchange " + th