使用JQuery在RadioButtonList上隐藏/显示面板的问题

时间:2012-07-05 06:40:51

标签: jquery .net

1)我的页面上有一个下拉列表机智选项“信用卡”和“发票/直接账单”。在选择“发票/直接账单”时,除了选择“我不需要”信用卡”: 2)我必须显示/隐藏radiobuttonlist(在Panel中:Panel2),它有3个选项(登记日期,预订日期,其他日期) 3)如果我点击radiobuttonlist的“Other Date”选项,我必须显示一个Textbox,它位于Panel里面:Panel3。 4)如果我点击radiobuttonlist的“Check-In Date”或“Book Date”选项,我必须隐藏Panel3内的文本框。

我的所有4个场景都在运作。 问题 : 如果我在下拉列表中选择“信用卡”,请在我的radiobuttonlist中选择“其他日期”选项,在我的文本框中输入值“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>

帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

您所看到的行为是有道理的。默认情况下,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');
});

答案 1 :(得分:0)

<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()
});