【问题标题】:Issue in Hiding/Showing a panel on RadioButtonList using JQuery使用 JQuery 在 RadioButtonList 上隐藏/显示面板的问题
【发布时间】:2012-07-05 06:40:50
【问题描述】:

1)我的页面上有一个下拉列表,选项为“信用卡”abd“发票/直接账单”。在选择“发票/直接账单”时,除了选择“信用卡”: 2) 我必须显示/隐藏 radiobuttonlist(inside a Panel:Panel2) ,它有 3 个选项(入住日期、预订日期、其他日期) 3)如果我点击 radiobuttonlist 的“其他日期”选项,我必须显示一个 Textbox ,它位于 Panel :Panel3 内。 4)如果我点击radiobuttonlist的“入住日期”或“预订日期”选项,我必须隐藏Panel3内的文本框。

我的所有 4 个场景都在工作。 问题 : 如果我在下拉列表中选择“信用卡”,在我的单选按钮列表中选择“其他日期”选项,在我的文本框中输入值“10”并单击提交按钮,回发发生并且我的值存储在数据库中。 当我重新加载页面时: 我在下拉列表中获得“信用卡”,在我的单选按钮列表中获得“信用卡”选项,但值为 15 的文本框不可见。当我选择“入住日期”/“预订日期”,然后选择“其他日期”时,我看到我的文本框的值为 15。代码如下:

<script type="text/javascript">
$(function () {
    $('select[id$=ddlCardType]').change(function () {
        if (this.value == -1) {
            $('div[id$=Panel1]').show();
            $('div[id$=Panel2]').hide();
            $('div[id$=Panel3]').hide();
        }
        else {
            $('div[id$=Panel1]').hide();
            $('div[id$=Panel2]').show();
        }
    }).change();

});
</script>
<script type="text/javascript">
$(document).ready(function () {
    var panel = $("#Panel3");
    var cbo = $("#Panel2").find("cboVisibility");
    $("#cboVisibility").find('input:radio').change(function (index) {
        //$("#Panel2 cboVisibility").find('input:radio').change(function (index) {
        //$("[id*=pnl2 cboVisibility input:radio]").change(function (index) {
        if ($(this).val() == "OD")
            panel.show();
        else
            panel.hide()
    });
    $('#cboVisibility').find('input:radio').trigger('change');
});
</script>
<asp:DropDownList ID="ddlCardType" runat="server" CssClass="arial11nr" Width="270px">
    <asp:ListItem Value="-1">Invoice/Direct Bill</asp:ListItem>
    <asp:ListItem Value="SUCC">Credit Card</asp:ListItem>
</asp:DropDownList>
<td align="left" valign="top">
    <asp:Panel ID="Panel1" runat="server" Style="display: none;">
        <strong>Billing Instructions/Notes</strong><span class="red-color">(optional)  
</span>
        <asp:TextBox ID="txtBillingInstructions" runat="server" TextMode="MultiLine">
 </asp:TextBox>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" Style="display: none;" ClientIDMode="Static">
        <asp:RadioButtonList ID="cboVisibility" CssClass="Normal" runat="server" 
 RepeatDirection="Vertical"
            ClientIDMode="Static">
            <asp:ListItem Value="CD" Selected="True">Check-In Date</asp:ListItem>
            <asp:ListItem Value="BD">Book Date</asp:ListItem>
            <asp:ListItem Value="OD">Other Date</asp:ListItem>
        </asp:RadioButtonList>
    </asp:Panel>
    <asp:Panel ID="Panel3" runat="server" Style="display: none;" ClientIDMode="Static">
        <strong>Charge</strong>
        <asp:TextBox ID="txtSUCCValidity" runat="server" ClientIDMode="Static" 
Width="50px"></asp:TextBox>
        <strong>Days Before Check-In</strong>
        <asp:RangeValidator ID="RangeValidator1" runat="server" 
ControlToValidate="txtSUCCValidity"
            ErrorMessage="<br />Not valid Range" MaximumValue="999"  
ValidationGroup="update"
            MinimumValue="0" Type="Integer" Display="Dynamic"></asp:RangeValidator>
    </asp:Panel>
</td>

我们将不胜感激

【问题讨论】:

    标签: jquery .net


    【解决方案1】:

    您看到的行为是有道理的。默认情况下 panel2 是隐藏的。仅当单选按钮触发更改事件并且选择第三个选项的更改时,您的 js 代码才会显示面板。当您在提交表单后加载页面时,单选按钮会在文档准备好之前设置为第三个选项。即,在可以订阅更改事件之前。这就是为什么您默认看不到文本框的原因。

    需要更改哪些内容才能启用您的方案,这取决于您如何设置单选按钮的值。

    一种解决方案是在文档就绪时触发更改事件。像这样

    $(document).ready(function () {
        var panel = $("#Panel2");
        var cbo = $("#Panel1").find("cboVisibility");
        $("#cboVisibility").find('input:radio').change(function (index) {
    
            // when triggering a change across all radio inputs, even 
            // those that are not selected will show up here. 
            // hence the check to verify if they are actually selected. 
            if (this.value == "OD" && this.checked) {  
                panel.show();
    
            // we wand to hide the panel3 only if an option other
            // than "OD" is selected.
            } else if (this.checked) {
                panel.hide(); 
            }
        });
    
        // this will trick the browser into thinking that you selected the third option
        $('#cboVisibility').find('input:radio').trigger('change');
    });
    

    【讨论】:

    • Amith George:如果我这样做,那么 Panel2 的内容显示在:单选按钮的“是”/“否”选项的选择以及当我在下拉列表中选择“B”时这不是我的要求。要求:仅当我们在下拉列表中选择“A”并且在 RadioButtonList 中选择“无”时,Panel2 才应该可见
    • 根据您发布的代码,如果用户单击是/否,则 $(this).val() 的值将是“CD”或“BD”而不是“OD”。仅当值为“OD”时才显示面板。如果它的行为最终不是这样,我建议您发布您实际使用的代码。我所做的唯一更改是在页面加载时触发更改事件。如果你仔细想想,这应该不会影响任何其他行为。
    • 最好只使用 html/css/js 创建一个 jsfiddle (jsfiddle.net)。这样我们两个就会在同一个页面上,没有双关语的意思。
    • Amith George :我刚刚发布了原始的 complate 代码。请看一下。
    • Amityh George,我已经根据代码编辑了描述,请看一下。
    【解决方案2】:
    <script type="text/javascript">
    var panel = $("#Panel3");
    var cbo = $("#Panel2").find("cboVisibility");
    
    $(document).ready(function () {
        //var ddlCardType = "<%=ddlCardType.ClientID %>";
        if ($('[id*=ddlCardType]>option:selected').val() == "SUCC" &&    
    $('[id*=cboVisibility] :checked').val() == "OD") {
            panel.show();
        }
    });
    
    
    $('select[id$=ddlCardType]').change(function () {
        if (this.value == -1) {
            $('div[id$=Panel1]').show();
            $('div[id$=Panel2]').hide();
            $('div[id$=Panel3]').hide();
        }
        else {
            $('div[id$=Panel1]').hide();
            $('div[id$=Panel2]').show();
            if ($('[id*=cboVisibility] :checked').val() == "OD")
                panel.show();
        }
    }).change();
    
    
    
    $("#cboVisibility").find('input:radio').change(function (index) {
        //$("#Panel2 cboVisibility").find('input:radio').change(function (index) {
        //$("[id*=pnl2 cboVisibility input:radio]").change(function (index) {
        if ($(this).val() == "OD")
            panel.show();
        else
            panel.hide()
    });
    

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 2011-03-27
      • 1970-01-01
      • 2011-07-28
      • 2017-12-08
      • 1970-01-01
      相关资源
      最近更新 更多