2011-09-18 75 views
0

我真正想要做的是在表格中的每個輸入框上運行一個函數。該表有一個類BudgetTable所以我雖然jquery text box keypress event

$(".BudgetTable tr input:text").keypress... 

會工作,但在VS2010的斷點沒有打。

因此,我創建了一個簡單的頁面,以確保它不是標記,所以我可以在這裏發佈一些不太複雜的內容。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Budget.aspx.cs" Inherits="BudgetApplicationWeb.Budget" %> 

<!DOCTYPE html PUBLIC "-//W3C//Dth XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/Dth/xhtml1-transitional.dth"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title></title> 
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> 
    <link href="Styles/BudgetGrid.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript"> 
    window.my_config = 
    { 
     last_section : "", 
     current_section_row:0 
    }; 

    $(document).ready(function() { 
     $("tr").each(function (index, domElem) { 
      var className = $(this).attr("class"); 
      if (className == window.my_config.last_section) { 

       window.my_config.current_section_row++; 

       if (window.my_config.current_section_row % 2 == 0) { 
        $(this).addClass("alt"); 
       } 

      } 
      else if (className != "") { 
       window.my_config.last_section = className; 
       window.my_config.current_section_row = 0; 
       if (window.my_config.last_section.toString().substr(0, 6) == "DataTR") { 
        $(this).addClass("alt"); 
       } 
      } 
     }); 
    }); 

    $("input").keypress(function (event) { 
     var className = $(this).attr("class"); 
     var str = ""; 

     str += "ClassName:" + className + "<BR>" 
     str += "ParentItem:" + className.toString().replace("Item", "") + "<BR>" 

     // $("#debug").text = str; 
     $("#debug").text = "hello"; 
    }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div id="debug"> 
     <asp:TextBox ID="TextBox3" runat="server" /> 
    </div> 
    </form> 
</body> 
</html> 

我看了後

How do I Select all text boxes in a table excluding the last one in the table row with JQuery

我想不通爲什麼按鍵不工作。

感謝您的幫助

+0

嘗試在輸入前添加一個冒號,讓你有$(「:input」)。按鍵.. – Ben

+0

似乎沒有做任何事情。我認爲:是爲類型或過濾器像奇數甚至不是元素? – Mike

回答

1

我看到一對夫婦的紅旗

  1. 你不在的document.ready
  2. 文本框的結合,這並不有一個類讓你的className線將無法正常工作
  3. className.toString()是多餘的

此外,你使用Firebug的喲你看到任何控制檯錯誤消息?

+0

2我並不擔心,因爲真正的頁面將有一個類,3我用toString去替換函數(至少在intellisense和1,以及我沒有看到任何綁定在這個例子中。http: //api.jquery.com/keydown/ – Mike

+0

在那個例子中,jQuery是在DOM元素之後出現的,如果你之前包含了它(並且沒有document.ready),它將立即執行,不會有DOM元素被綁定到 –

+0

在firebug中沒有錯誤,但是當我鍵入$(「input」)。keypress我得到了「function()」空函數 – Mike