2012-08-09 130 views
-1

您好我想空格键在JavaScript中没有文本框中按空格键被解雇,我想这段代码,请帮我解决这个调用按键事件,而不在JavaScript实际上按下键

<script language="javascript" type="text/javascript"> 
     jQuery(document).ready(function() { 
      var getOldValue = jQuery("#<%=txtCompany.ClientID%>"); 
       var e = jQuery.Event("keydown"); 
       e.which = 32; //key code 
       jQuery("#<%=txtCompany.ClientID%>").trigger(e); 

     }); 
    </script> 

和ASPX页面

<asp:TextBox ID="txtCompany"></asp:TextBox> 
+0

你问问题的Javascript和使用代码jQuery的,这是误导响应者。 – 2016-04-29 07:06:16

回答

0

您正在客户端脚本上使用服务器端标记。这是行不通的。相反,ASP将创建一个id等于txtCompany的HTML输入框。所以让我们使用它。

jQuery(document).ready(function() { 
    var inputBox = jQuery("#txtCompany"), 
     getOldValue = inputBox.val(), 
     e = jQuery.Event("keydown"); 
    e.which = 32; //key code 
    inputBox.trigger(e); 
}); 

我没有做过的事件中,你是用jQuery.Event(的方式触发),但如果这是有效的代码,那么这应该解决您的问题。

+0

但我需要使用asp控件(文本框)只..我们不能做任何事情? – user1527989 2012-08-09 10:07:21

+0

asp控件变成HTML。在页面HTML中,asp控件成了什么? ''?如果是这样,请尝试我的方法。 。如果没有,请使用浏览器调试器进行检查。 – 2012-08-09 10:12:58

+0

不,我没有得到如果我改变一切到html控制。 – user1527989 2012-08-09 14:32:04

0

u可以使用initKeyEvent和jQuery的

var e = jQuery.Event("keydown"); 
e.which = 50; //key code 
$("#some_element").trigger(e); 

和香草的javascript,这是它如何工作的:

// Create the event 
var evt = document.createEvent('KeyboardEvent'); 

// Init the options 
event.initKeyEvent(
     "keypress",  // the kind of event 
      true,    // boolean "can it bubble?" 
      true,    // boolean "can it be cancelled?" 
      null,    // specifies the view context (usually window or null) 
      false,   // boolean "Ctrl key?" 
      false,   // boolean "Alt key?" 
      false,   // Boolean "Shift key?" 
      false,   // Boolean "Meta key?" 
      9,    // the keyCode 
      0);    // the charCode 

// Dispatch the event on the element 
el.dispatchEvent(evt); 
+0

你能解释如何通过在文本框中使用空格键代码来生成空间吗? – user1527989 2012-08-09 10:42:03

+0

最后一行是什么? – user1527989 2012-08-09 10:48:47

+0

http://www.ehow.com/info_12195163_spacebar-key-codes-javascript.html – 2012-08-09 11:28:39

相关问题