执行下面的代码时,它会先显示文本框的原始内容,然后显示每组键我一直按下,直到点击“Enter”。因此,如果文本框最初包含“冰淇淋 - 皮埃尔的巧克力片”和我在文本框中单击以获得焦点,并与“茶”取代它,我会得到4个警告框,每个具有以下内容:尝试在按下Enter键后显示文本框内的内容
- 冰淇淋 - 皮埃尔的巧克力片
- 牛逼
- 特
- 茶
然后,如果我点击不同的行和做其他的事情,我会从之前然后原来的4个警报框新的...它k eeps堆叠,我不知道为什么?
这里是我的cs文件的一些代码 - 注意“的OnKeyPress”属性我说:
public TCell(String celltext,String strTextBox,String strName)
{
int iWidth = 0;
int intRandom =
iWidth = Convert.ToInt32(strTextBox);
tblCell = new TableCell();
TextBox txtItem = new TextBox();
txtItem.ID = strName;
txtItem.Text = celltext;
txtItem.Width = iWidth;
txtItem.BorderStyle = BorderStyle.None;
txtItem.CssClass = "cssAdjust trans";
txtItem.Style.Add("padding", "0px 5px 0px 5px");
txtItem.Attributes.Add("onKeyPress","captureTable(this.value);");
txtItem.Attributes.Add("onFocus", "javascript:this.select();");
tblCell.Controls.Add(txtItem);
}
然后这里是我曾经显示的输入键被按下的功能:
function captureTable(strValue)
{
$(document).keypress(function (e) {
if (e.which == 13) // Enter has been pressed.
{
e.preventDefault();
document.getElementById("<%=HiddenField1.ClientID %>").value = strValue;
alert(strValue);
}
});
在此先感谢您提供的任何帮助。我有这个工作使用“window.event”,但从我读的内容锁定到IE浏览器,我试图使用JQuery。
(UPDATE):
好吧,我使用下面kmb385提供的代码,但我想我失去了一些东西还在。我没有看到任何警报,所以我在'$(“.txtHook”).presspress(function(e){'line')处设置了一个断点,并且在表格文本框内的键输入期间它没有到达它包括回车键
这里就是代码所在的default.apsx页面中:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
$(document).ready(function(){
$(".txtHook").keypress(function(e){
if (e.which == 13) // Enter has been pressed.
{
e.preventDefault();
document.getElementById("<%=HiddenField1.ClientID %>").value = $(this).val();
alert(strValue);
}
});
});
我的jQuery包容是该网站的母版页的body标签内:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.4.1.min.js" />
</Scripts>
</asp:ScriptManager>
(FINAL UPDATE :)
原来我包含的JQuery不正确。我把它移到母版页的head标签和更改的代码如下:
<script type="text/javascript" src="../scripts/jquery-1.4.1.min.js"></script>
之后,提供的代码我kmb385开始工作了:)
你想捕捉,当他们按下回车键而输入字段有专注或只是当他们按Enter键? – 2012-04-14 23:19:45
输入键被按下时,无论哪个文本框都有焦点。如果他们击中输入,并专注于他们刚才制作的单选按钮,我希望被忽略。我的屏幕由2个下拉列表,一个表格,3个单选按钮以及基于表格中选定行显示信息的各种标签组成。我正在使用这些警报来测试我的逻辑,以捕获被锁定到文本框中的内容。谢谢。 – 2012-04-14 23:31:53
好的,这有助于我回答你的问题。 – 2012-04-14 23:34:25