2013-03-10 41 views
0

我想要做的是模拟用户在提示符下键入内容并按下回车键。javascript当文本输入只包含特定字符时执行

活页是http://www.certified-computer-service.com

代码我现在有一个看起来像这样:

<script type="text/javascript"> 
function buttons(e){ 
    var unicode=e.keyCode? e.keyCode : e.charCode; 
    switch(unicode){ 
     case 49: 
      window.alert("1"); 
      break; 
     case 50: 
      window.alert("2"); 
      break; 
     case 51: 
      window.alert("3"); 
      break; 
     case 52: 
      window.alert("4"); 
      break; 
     case 53: 
      window.alert("5"); 
      break; 
     case 97: 
      window.alert("1"); 
      break; 
     case 98: 
      window.alert("2"); 
      break; 
     case 99: 
      window.alert("3"); 
      break; 
     case 100: 
      window.alert("4"); 
      break; 
     case 101: 
      window.alert("5"); 
      break; 
    } 
} 
</script> 

blah blah 

<input type="text" name="selection" id="selection" onkeydown="buttons(event); this.select()" style="border: none; background-color: #C0C0C0;"> 

我怎么能解决这个问题,使其工作打算?虽然下面的代码将无法正常工作,而不是真正的语言编写,它是表示什么,我心里有:

inputbox.onKeyup(theKey){ 
    if theKey == "enter" && inputbox.value == "1" { //respond to key 1 } 
    if theKey == "enter" && inputbox.value == "2" { //respond to key 2 } 

,我打算进一步澄清可以通过查看底部中扣除所提到的网站。

+0

“?我怎么能解决这个问题,使按预期工作,”这是根本不清楚你想要做什么。请尽量清楚说明你希望做什么。我们无法读懂你的想法。 – 2013-03-10 21:03:13

+1

如果你想让键盘上的按键“1”触发一个特定的链接,为什么不从你已经拥有的javascript中做到这一点(例如'window.location = link [inputbox.value];') – Offbeatmammal 2013-03-10 21:04:43

+0

ben33 - 我用不同的方式说了三次,我试图完成。一旦在帖子的顶部,一次使用“不是真正的代码”代码,并且如果你仍然没有得到它,再次看看网站。此外,这里的其他人已经发布了,他们似乎确切知道我的目标是什么。不是我的错,你仍然不明白。 Offbeatmammal - 是的,一旦创建其他页面,我就会这么做。 – bradboy 2013-03-11 00:39:01

回答

0

怎么是这样的:

var code = (e.keyCode ? e.keyCode : e.which); 
if(code == 13) { //Enter keycode 
//get the text from the div with jQuery or document.getElementById 
var input = jQuery('#selection').val() 
//you can use a json to map your results or the switch as you described. 
//json example: 
var json = {1: 'hello', 2: 'world'} 
json['1'] 
} 
+0

ShiranR - 谢谢!它只需要一点修补,但我不需要谷歌任何东西! – bradboy 2013-03-11 01:12:28

相关问题