【问题标题】:Radcombobox hide and display using javascriptRadcombobox 使用 javascript 隐藏和显示
【发布时间】:2014-08-12 21:49:43
【问题描述】:

我有一个 div2,只有当 RadioButtonList1 的值为“是”时,我才会通过 javascript 函数 getvalue().Initially 在 div1 中显示它我没有使用 c# 代码显示 div2。

<fieldset id="disc" class="nt_generic">
    <div runat="server" id="div1">
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"
            OnClick="getvalue()">
            <asp:ListItem Value="Yes" Text="Yes" />
            <asp:ListItem Value="No" Text="No" Selected="True" />
        </asp:RadioButtonList> 
    </div>

    <div id="div2" runat="server">
        <radC:RadComboBox ID="rcb1" EnableLoadOnDemand="true" runat="server" Skin="WebBlue">
        <Items>
            <radC:RadComboBoxItem ID="CondoComboBoxItem" runat="server" Text="A Ground" Value="B" Selected="true" />
            <radC:RadComboBoxItem ID="HomeComboBoxItem" runat="server" Text="B Ground" Value="A" />                            
        </Items>
        </radC:RadComboBox> 
    </div>
</fieldset>

function getvalue()
{
    var value = $('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val(); 
    if(value == "Yes")
    {
        if (document.getElementById("<% = div2.ClientID %>") != null)
            document.getElementById("<%= div2.ClientID %>").style.display = "inline-block";
    }
    else
        if (document.getElementById("<% = div2.ClientID %>") != null)
           document.getElementById("<%= div2.ClientID %>").style.display = "none";
}

当我按照说明使用上述代码时,div2 首先显示并隐藏我不想要的后记。

如果我像下面这样将visible=false 添加到 div2

<div id="divTankLocated" runat="server" visible="false">

div2没有隐藏和显示,但是javascript函数getvalue()不起作用,并且不能在RadioButtonList1值改变时显示div2。

可以帮助我解决以上 2 点吗?

【问题讨论】:

    标签: javascript asp.net radcombobox


    【解决方案1】:

    至少部分问题是由于 javascript 的自动分号插入造成的。

    例如:

    if(value == "Yes")
    {
    }
    

    变成:

    例如:

    if(value == "Yes");
    {
    }
    

    这意味着每次都会执行 if 语句的 then 部分。为了防止这种情况发生,您必须将开头的{ 放在前一行。这样做并将 { } 对添加到所有其他 if 语句中,将您的 javascript 变成这样:

    function getvalue() {
        var value = $('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val(); 
        if(value == "Yes") {
            if (document.getElementById("<% = div2.ClientID %>") != null) {
                document.getElementById("<%= div2.ClientID %>").style.display = "inline-block";
            }
        } else if (document.getElementById("<% = div2.ClientID %>") != null) {
               document.getElementById("<%= div2.ClientID %>").style.display = "none";
        }
    }
    

    【讨论】:

    • 像这样添加分号后 if(value == "Yes");获取 JavaScript 错误消息,因为值未定义并且添加 {} 也不适合我。
    • 我在 body_onload() 函数中调用 getvalue() 函数,它开始按预期为我工作。谢谢大家。
    • 我没有得到您在答案开头描述的行为。我做了一个小提琴来测试它(jsfiddle.net/f3kpfmer/1)。是否有特定版本的 JavaScript 表现出这种行为,还是我遗漏了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2017-02-11
    • 2016-02-03
    • 2018-12-14
    相关资源
    最近更新 更多