繁体   English   中英

根据RadioButtonList选择的值隐藏/显示Div标签

[英]Hide/Show Div Tag based on RadioButtonList selected value

我有一种情况,我必须根据复选框选择显示/隐藏DIV TAG。 下面是代码示例

<asp:RadioButtonList ID="ckbRestaurent" runat="server"  RepeatDirection="Horizontal" RepeatLayout="Flow" >   
     <asp:ListItem selected="true" Value="0">No</asp:ListItem>  
     <asp:ListItem Value="1">Yes</asp:ListItem> 
</asp:RadioButtonList>

<div id="xyz"> something.. </div>

我尝试了几种方法,但它不起作用,因为我是jQuery的新手,如果有人能给我一个上述代码的工作示例,我将不胜感激。

默认情况下,应该在页面加载时隐藏div,并在用户选择RadioButtonList中的YES选项时显示DIV

因为我想在我的问题中使用精确的HTML节目解决方案。 所以我发现了一个解决方案

<script language="javascript" type="text/javascript">
   $(document).ready(function () {
     $('#RadioDiv input').click(function () {
     $("#info").text('Selected Value: ' + $("#RadioDiv input:radio:checked").val());
         if ($("#RadioDiv input:radio:checked").val() == 0) {
              document.getElementById('Restaurent').style.display = "none";
              }
              else {
              document.getElementById('Restaurent').style.display = "";
                    }
       });
     }); 
</script>

HTML代码如下

<div id="RadioDiv">
<asp:RadioButtonList ID="ckbRestaurent" runat="server"  RepeatDirection="Horizontal" RepeatLayout="Flow" >   
  <asp:ListItem selected="true" Value="0">No</asp:ListItem>  
  <asp:ListItem Value="1">Yes</asp:ListItem> 
</asp:RadioButtonList>
</div>
<table class="style1">
      <tr>
         <td ></td><td></td>
      </tr>
      <tr><td ></td><td></td>
       </tr>
</table>
    </br>
<div id="Restaurent" style="display:none" >
<!-- Details Group Restaurent Table -->
<table cellpadding="0" cellspacing="0" class="tableDetailsGroupOne">
  <tr>
      <td class="tableDetailsGroupOneLabel">Restaurant One:</td>
      <td>
      <asp:TextBox ID="txtRestaurentOne" runat="server" CssClass="txtNameOfHotel">Restaurent One</asp:TextBox>
      </td>
</tr>
<tr>
      <td class="tableDetailsGroupOneLabel">Restaurent Two:</td>
      <td>
<asp:TextBox ID="txtRestaurentTwo" runat="server" CssClass="txtNameOfHotel" ></asp:TextBox>
      </td>
</tr>
</table>
</div>
<!-- Details Group Restaurent Table END-->

我也感谢其他用户也回答了我的问题,因为我想要使用ASP.NET服务器控件的解决方案。

你需要将客户端javascript调用挂钩到你的RadioButtonList。 在javascript函数中,你可以做类似$(“#xyz”)。hide()或$(“#xyz”)。show()

试试这段代码

$(document).ready(function(){ 
        $("#xyz").hide();
        $("input[type=checkbox]").click(function (){
            if ($(this).attr('checked')){
                $("#xyz").show();
            }
            else {
                $("#xyz").hide();
            }
        });

    });

并添加您的HTML代码,以便我们可以修改上面的脚本

我正在添加你提到的html版本。 试着理解并让我知道。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
    $(document).ready(function(){ 
        $("#xyz").hide();
        $("input[type=checkbox]").click(function (){            
            if ($(this).val()=='0' && $(this).attr('checked')){
                $("#xyz").show();
            }
            else {
                $("#xyz").hide();
            }
        });

    });
</script>
</head>
<body>
<input type="checkbox" value="0">yes
<input type="checkbox" value="1">no
<div id="xyz"> something.. </div> 
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM