列表视图中的文本框

textbox in a listview

本文关键字:文本 视图 列表      更新时间:2023-09-26

我有一个ListView,包含一个下拉,4个文本框和按钮。我希望显示第四个文本框(这是包装在一个跨度)只有当下拉框是ListView的每一行的value=2

例如,我在ListView中显示了5条记录,第二和第三条都是value=2

这是在我的document.ready中,它预设并选择页面加载时下拉框的每个值。所以我试图显示ZipBox当它这样做的时候,我觉得这将是最简单的,但我也愿意接受其他建议,因为显示框似乎不起作用。

$('.Existing').each(function() {
    var select = $(this).attr('data');
    $(this).val(select);
    //this part doesn't work below
    if(select=="2")
    {
        $('#zipBox').css({ 'visibility': 'visible' });
}
});

我还需要显示文本框,如果下拉选择被改变,见下面尝试了这个,但它只适用于第一个。例如,如果我改变第5条记录的值,它将使ZipBox在第一行可见,而不是在第5行。

$('.Existing').change(function() {
    if ($(this).val() == '2') {
        $('#ZipBox').css({ 'visibility': 'visible' });
    }
});

listview:

<asp:ListView runat="server" ID="ListView1">
    <LayoutTemplate>
        <table id="tablesorter" style="border: solid 1px black; width: 55%;">
            <thead>
                <tr>
                    <th>
                        <a href="#">Country</a>
                    </th>
                    <th>
                        <a href="#">Info.</a>
                    </th>
                    <th>
                        <a href="#">Action</a>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr id="itemPlaceholder" runat="server" />
            </tbody>
            <tfoot>
            </tfoot>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                &nbsp;&nbsp;<select id="Existing" data="<%# Eval("Type").ToString()%>"
                    class="Existing" style="width: 90px">
                    <option value="0">USA</option>
                    <option value="1">CAN</option>
                    <option value="2">MEX</option>
            </td>
            <td>
                <input size="4" type="text" id="city" value="<%# Eval("City")%>" />
                <input size="4" type="text" id="state" value="<%# Eval("State")%>" />
                <input size="4" type="text" id="Phone" value="<%# Eval("PhoneNbr")%>" />
                <span id="ZipBox" style="visibility: hidden">
                    <input maxlength="5" size="5" type="text" id="zip" value="<%# Eval("ZIP")%>" />
                </span>
            </td>
            <td>
                <2 buttons here>
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>

输入可以切换(与visibility),所以跨度是不必要的,但要切换跨度或输入class属性将需要代替id (id应该是唯一的,所以使用#ZipBox多个zip字段将不起作用)

jsfiddle演示

在您的第一次通过(.each()),这将设置zip可见性,我注释了将下拉菜单的值设置为data属性的部分,因为这会中断检查value = 2

$('.Existing').each(function () {
    var $this = $(this), // drop down
        $thisRow = $this.closest('tr'), // the drop down's row
        $thisZip = $thisRow.find('.zip'), // the zip textbox on the same row
        $thisDataType = $this.attr('data'); // data attribute contains the type
    //$(this).val(select); // type is being set to the value
    if ($this.val() == 2) {
        $thisZip.css({'visibility': 'visible'});
    }
});

对于更改事件,您可以做几乎相同的事情,但添加了else {}块,如果MEX被选中,然后更改zip将再次隐藏

$('.Existing').change(function () {
    var $this = $(this), // the drop down that changed
        $thisRow = $this.closest('tr'), // the drop down's row
        $thisZip = $thisRow.find('.zip'); // the zip textbox on the same row
    if ($this.val() == 2) {
        $thisZip.css({'visibility': 'visible'});
    } else {
        $thisZip.css({'visibility': 'hidden'});
    }
});