2010-07-21 49 views
0

我怎樣才能在JavaScript或jQuery的多視圖?MultiView和JavaScript(或jQuery)工作

下面的代碼始終返回null:(JavaScript)的

var MultiView = document.getElementById("MultiView1"); 

和下面的代碼不爲空,但不工作:(jQuery的)

var MultiView = $("*[id$='TextBox1']"); 

什麼關於這回事?

你可以給我一個示例代碼來檢查ActiveViewIndex使用JavaScript或Jquery!

我加入了下面的代碼,因爲評論的:在未來的提前

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Keyup._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

    <html xmlns="http://www.w3.org/1999/xhtml" > 
    <head runat="server"> 
     <title></title> 

    <%-- <script src="JQuery/jquery-1.4.1.js" type="text/javascript"></script>--%> 
       <script type="text/javascript"> 

        document.onkeyup = onkeyupOfDocument; 

        function onkeyupOfDocument(evt) { 
         var evt = evt || window.event; 
         //alert(evt.keyCode); 
         //var MultiView = $("*[id$='TextBox1']"); 
         var MultiView = document.getElementById("MultiView1"); 
         alert(MultiView); 
        } 

     </script> 
    </head> 
    <body> 
     <form id="form1" runat="server"> 
     <div> 
      <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> 
       <asp:View ID="View1" runat="server"> 
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
       </asp:View> 
       <asp:View ID="View2" runat="server"> 
       </asp:View> 
      </asp:MultiView> 
     </div> 
     </form> 
    </body> 
    </html> 

感謝

+0

你能否提供你想要選擇的元素的HTML標記?在不知道標記是什麼樣的情況下,無法判斷選擇器是否正確。 – user113716 2010-07-21 13:58:42

+0

我添加aspx(html)代碼,如你所說... – MoonLight 2010-07-21 14:45:24

回答

0

很抱歉的延遲,但這裏是你在找什麼:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(function() { 

      var activeViewIndex = $('.multiviewContainer').attr('activeKey'); 

      alert(activeViewIndex); 

     }); 
    </script> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 

    <asp:Panel runat="server" ID="multiviewContainer" CssClass="multiviewContainer"> 

     <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> 

      <asp:View ID="View1" runat="server"> 
        View 0 
      </asp:View> 

      <asp:View ID="View2" runat="server"> 
        View 1 
      </asp:View> 

      <asp:View ID="View3" runat="server"> 
        View 2 
      </asp:View> 

      <asp:View ID="View4" runat="server"> 
        View 3 
      </asp:View> 

     </asp:MultiView> 

    </asp:Panel> 

</asp:Content> 

和代碼後面:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string activeView = MultiView1.ActiveViewIndex.ToString(); 
     multiviewContainer.Attributes.Add("activeKey", activeView); 
    } 
} 
+0

真的非常感謝/但我的目標是不改變多視圖activeviewindex /我只想檢查多視圖ActiveViewindex做的事情...(我不能改變多視圖與div - 因爲我在代碼中多次使用multiview) – MoonLight 2010-07-21 14:14:32