【问题标题】:Obtain value from Gridview using JS使用JS从Gridview获取值
【发布时间】:2016-09-04 23:34:01
【问题描述】:

所以我学会了如何通过聚焦 gridview 单元格来弹出警报,但我不确定如何让它显示单元格的值。 这是我的代码:

<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>

脚本如下:

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

问题是这给了我列中第一个单元格的值。不是我关注的那个。

【问题讨论】:

    标签: javascript asp.net gridview


    【解决方案1】:

    当您执行$('.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);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多