入门列经过的GridView中使用jQuery [英] Getting Columns Checked in GridView with jQuery

查看:55
本文介绍了入门列经过的GridView中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView和我需要抢列1和2(文字文献),并将该文本在其他地方的页面,另一个文本框。

下面是我迄今为止:

  VAR检查= $('输入:复选框)点击(函数(五){
            //在文本框,txtAirport地方机场
            VAR LOC = $(TR:有(:复选框:选中)TD:第n个孩子(1));
            VAR AC = $(TR:有(:复选框:选中)TD:第n个孩子(2));            // txtAirport.Text = LOC +交流;
            $(#&下;%= txtAirport.ClientID%gt;中)。VAL(LOC + AC);
        });

问题是我得到{Object对象} {Object对象}在我的文本框,我很期待得到一个城市,ST和机场。我怎么澄清我的禄和交流变量给我我需要什么?我试过的.text和.VAL()无济于事。

下面是HTML(ASP.NET的GridView):

 < ASP:DataGrid的ID =dgSearch=服务器AllowPaging =FALSEAllowSorting =FALSE
                            CELLPADDING =3CELLSPACING =2ShowFooter =假的网格线=无的AutoGenerateColumns =假>
                            < AlternatingItemStyle裹=FALSE的CssClass =HeaderRowAlternate>< / AlternatingItemStyle>
                            < ItemStyle环绕=FALSE>< / ItemStyle>
                            < HeaderStyle字体粗体=真总结=FALSE的CssClass =HeaderRowStyle>< / HeaderStyle>
                            <柱体和GT;
                                < ASP:TemplateColumn中的HeaderText =选择>
                                    < HeaderStyle的CssClass =HEADERSTYLE>< / HeaderStyle>
                                    <&ItemTemplate中GT;
                                        < ASP:复选框ID =chkLookup=服务器的CssClass =复选框>< / ASP:复选框>
                                    < / ItemTemplate中>
                                < / ASP:TemplateColumn中>
                                < ASP:绑定列可见=假的DataField =AirportID>< / ASP:绑定列>
                                < ASP:绑定列可见=假的DataField =城与GT;< / ASP:绑定列>
                                < ASP:绑定列的DataField =禄的HeaderText =位置>
                                    < HeaderStyle换行=FALSE的CssClass =HEADERSTYLE>< / HeaderStyle>
                                    < ItemStyle环绕=FALSE>< / ItemStyle>
                                < / ASP:绑定列>
                                < ASP:绑定列的DataField =机场code的HeaderText =机场code>
                                    < HeaderStyle换行=FALSE的CssClass =HEADERSTYLE>< / HeaderStyle>
                                    < ItemStyle环绕=FALSE>< / ItemStyle>
                                < / ASP:绑定列>
                                < ASP:绑定列的DataField =AirportName的HeaderText =机场名称>
                                    < HeaderStyle换行=FALSE的CssClass =HEADERSTYLE>< / HeaderStyle>
                                    < ItemStyle环绕=FALSE>< / ItemStyle>
                                < / ASP:绑定列>
                                < ASP:绑定列的DataField =MilesFromSource的HeaderText =距离>
                                    < HeaderStyle换行=FALSE的CssClass =HEADERSTYLE>< / HeaderStyle>
                                    < ItemStyle环绕=FALSE>< / ItemStyle>
                                < / ASP:绑定列>
                            < /专栏>
                        < / ASP:DataGrid的>


解决方案

jQuery选择返回包含选定的DOM元素包裹套,所以你需要使用的.text()或.html()

编辑:好了,我复制你的code,你需要的是

  $(TR:有(:复选框:选中)TD:第n个孩子(2));
$(TR:有(:复选框:选中)TD:第n个孩子(3));

然后做

  $(#<%= txtAirport.ClientID%GT;)。VAL(LOC [0] .innerHTML + AC [0] .innerHTML);

这将会给你的第一行中的第一和第二列的文本(复选框后)(LOC [1]和AC [1]为第二排等)。你有这个问题是 TD:第n个孩子(0)在返回的DOM对象的复选框的< TD> 元素

要通过选中的复选框仅循环:

 为(VAR I = 0; I< loc.length;我++){
    警报(LOC [I] .innerHTML ++ AC [I] .innerHTML);
}

您不需要检查复选框被选中,因为jQuery选择已经做了你。

I have a GridView and am needing to grab column 1 and 2 (the text therein) and put that text elsewhere in the page, another textbox.

Here is what I have thus far:

 var checked = $('input:checkbox').click(function(e) {
            //place airport in textbox, txtAirport
            var loc = $("tr:has(:checkbox:checked) td:nth-child(1)");
            var ac = $("tr:has(:checkbox:checked) td:nth-child(2)");

            // txtAirport.Text = loc + ac;
            $("#<%= txtAirport.ClientID%>").val(loc + ac);
        });

the problem is I am getting {Object object} {Object object} in my textbox where I'm expecting to get a city, st and an airport. How do I clarify my "loc" and "ac" variables to give me what I need? I've tried .text and .val() to no avail.

Here is the HTML (ASP.NET Gridview):

 <asp:DataGrid ID="dgSearch" runat="server" AllowPaging="False" AllowSorting="False"
                            CellPadding="3" CellSpacing="2" ShowFooter="False" GridLines="None" AutoGenerateColumns="false">
                            <AlternatingItemStyle Wrap="False" CssClass="HeaderRowAlternate"></AlternatingItemStyle>
                            <ItemStyle Wrap="False"></ItemStyle>
                            <HeaderStyle Font-Bold="True" Wrap="False" CssClass="HeaderRowStyle"></HeaderStyle>
                            <Columns>
                                <asp:TemplateColumn HeaderText="Select">
                                    <HeaderStyle CssClass="HEADERSTYLE"></HeaderStyle>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chkLookup" runat="server" CssClass="checkbox"></asp:CheckBox>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:BoundColumn Visible="False" DataField="AirportID"></asp:BoundColumn>
                                <asp:BoundColumn Visible="False" DataField="City"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Loc" HeaderText="Location">
                                    <HeaderStyle Wrap="False" CssClass="HEADERSTYLE"></HeaderStyle>
                                    <ItemStyle Wrap="False"></ItemStyle>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="AirportCode" HeaderText="Airport Code">
                                    <HeaderStyle Wrap="False" CssClass="HEADERSTYLE"></HeaderStyle>
                                    <ItemStyle Wrap="False"></ItemStyle>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="AirportName" HeaderText="Airport Name">
                                    <HeaderStyle Wrap="False" CssClass="HEADERSTYLE"></HeaderStyle>
                                    <ItemStyle Wrap="False"></ItemStyle>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="MilesFromSource" HeaderText="Distance">
                                    <HeaderStyle Wrap="False" CssClass="HEADERSTYLE"></HeaderStyle>
                                    <ItemStyle Wrap="False"></ItemStyle>
                                </asp:BoundColumn>
                            </Columns>
                        </asp:DataGrid>

解决方案

jQuery selectors return wrapped sets containing the selected DOM elements, so you'll need to use .text() or .html()

EDIT: OK, I've replicated your code, what you need is

$("tr:has(:checkbox:checked) td:nth-child(2)");
$("tr:has(:checkbox:checked) td:nth-child(3)");

And then to do

$("#<%= txtAirport.ClientID%>").val(loc[0].innerHTML + ac[0].innerHTML);

This will give you the text in the first and second columns (after the checkbox) of the first row (loc[1] and ac[1] for second rows etc.). The issue you had was td:nth-child(0) was returning the DOM object of the checkbox's <td> element

To loop through checked checkboxes only:

for (var i = 0; i < loc.length; i++) {
    alert(loc[i].innerHTML + " " + ac[i].innerHTML);
}

You don't need to check the checkbox is checked since the jQuery selector already does that for you

这篇关于入门列经过的GridView中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆