0

我有一个ListView包含一个下拉列表、4 个文本框和按钮。我希望仅在下拉列表value=2针对ListView.

例如,我有 5 条记录显示在ListView第 2 条和第 3 条中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' });
    }
});

这是列表视图:

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

1 回答 1

0

可以切换输入(使用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'});
    }
});
于 2013-03-07T16:13:15.653 回答