Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS从Gridview获取值_Javascript_Asp.net_Gridview - Fatal编程技术网

Javascript 使用JS从Gridview获取值

Javascript 使用JS从Gridview获取值,javascript,asp.net,gridview,Javascript,Asp.net,Gridview,所以我已经学会了如何通过聚焦gridview单元格来弹出警报,但我不知道如何让它显示单元格的值。 这是我的密码: <asp:GridView ID="gridviewSLds" runat="server" CellPadding="0" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="False" OnRowCreated="gridviewSLds_RowCreated"> <Alternating

所以我已经学会了如何通过聚焦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>
问题是,这会给我列中第一个单元格的值。不是我关注的对象。

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