仅在按下回车键后才尝试在动态表格中显示文本框的内容

时间:2012-04-14 23:07:16

标签: jquery

当我执行以下代码时遇到的问题,它将首先显示文本框的原始内容,然后是我按下的每组键,直到命中“Enter”。因此,如果文本框最初包含“Ice Cream - Pierre's Chocolate Chip”,我点击文本框以获得焦点并将其替换为“Tea”,我将获得4个警告框,每个框都包含以下内容:

  1. 冰淇淋 - 皮埃尔巧克力片
  2. Ť
  3. 然后,如果我点击另一行并执行其他操作,我将从之前获得原始的4个警告框,然后是新的...它会保持堆叠,我不知道为什么?

    以下是我的.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”)设置断点。按键(函数(e){'行,并且在表文本框内的键输入时它没有到达它,包括回车键。

    以下是代码位于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>
        <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>
    

    (最终更新:)

    原来我的JQuery包含不正确。我将其移动到母版页的head标签,并将代码更改为:

    <script type="text/javascript" src="../scripts/jquery-1.4.1.min.js"></script>
    

    之后,代码提供了我的kmb385开始工作:)

1 个答案:

答案 0 :(得分:0)

您的原始javascript代码将匿名函数绑定到文档上的按键事件,每次在您创建的输入中按下某个键。这导致每次在文档/页面上的任何位置按下键时都会触发多个函数。我删除了绑定到.cs文件中的onKeyPress事件。同样在cs文件中我向文本框添加了一个class属性,所以我可以在jquery中挂钩它。

在Javascript中,我使用jquery中的类选择器将函数绑定到每个输入的keypress事件。该函数获取输入的值并将其分配给隐藏的输入。这段代码可能存在问题,因为我不熟悉.net,如果让我知道并且我们可以解决它们。我对以下选择器持怀疑态度,但我猜它是一个.net的东西。

  

document.getElementById(“&lt;%= HiddenField1.ClientID%&gt;”)。value

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 = "txtHook cssAdjust trans";
    txtItem.Style.Add("padding", "0px 5px 0px 5px");
    txtItem.Attributes.Add("onFocus", "javascript:this.select();");
    tblCell.Controls.Add(txtItem);
}

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);
            }
        });
    });