2016-09-04 12 views
0

グリッドビューのセルに焦点を当てて警告をポップアップする方法を学習しましたが、セルの値を表示する方法がわかりません。JSを使用してGridviewから値を取得

$(document).ready(function(){ 
    $('.alertpopup').focus(function() { 
     var itemvalue = $('.alertpopup').val(); 
     alert(itemvalue); 
    }); 
}); 

問題は、これが私の列の最初のセルの値を与えることである。

<asp:GridView ID="gridviewSLds" runat="server" CellPadding="0" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="False" OnRowCreated="gridviewSLds_RowCreated"> 
    <AlternatingRowStyle BackColor="White" /> 
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
    <SortedAscendingCellStyle BackColor="#FDF5AC" /> 
    <SortedAscendingHeaderStyle BackColor="#4D0000" /> 
    <SortedDescendingCellStyle BackColor="#FCF6C0" /> 
    <SortedDescendingHeaderStyle BackColor="#820000" /> 
    <Columns> 
     <asp:TemplateField ItemStyle-BorderWidth="0"> 
      <ItemTemplate> 
       <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Id") %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="item" HeaderText="Metric" SortExpression="item" ReadOnly="false" /> 
     <asp:TemplateField HeaderText="Item"> 
      <ItemTemplate> 
       <asp:TextBox onfocusin="select()" runat="server" Text='<%# Bind("item") %>' 
        ID="txtfocus" class="alertpopup" AutoPostBack="true"></asp:TextBox> 
      </ItemTemplate> 
      <HeaderStyle HorizontalAlign="Center" /> 
      <ItemStyle HorizontalAlign="Center" /> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Control Type"> 
      <ItemTemplate> 
       <asp:TextBox onfocusin="select()" runat="server" Text='<%# Bind("itemCtrlType") %>' 
        ID="txtfocus2" class="modalpopup2" AutoPostBack="true"></asp:TextBox> 
      </ItemTemplate> 
      <HeaderStyle HorizontalAlign="Center" /> 
      <ItemStyle HorizontalAlign="Center" /> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

スクリプトは次のとおりです。 はここに私のコードです。私が焦点を当てたものではありません。

答えて

2

$('.alertpopup').val();$('.alertpopup')はセレクタに一致するすべての要素を選択しますが、最初に選択した要素だけを使用します。ただし、コールバック関数のthisコンテキストは、フォーカスされたHTML要素に設定されています。だから、代わりにこれを行うことができ、それは動作します:

$(document).ready(function(){ 
    $('.alertpopup').focus(function() { 
     var itemvalue = $(this).val(); // By selecting `this` you select the focused element 
     alert(itemvalue); 
    }); 
}); 
関連する問題