duancan65665 2017-11-29 07:07
浏览 195
已采纳

如何根据按钮的值显示/隐藏Div on click事件

Am working on mock test portal project, Can you suggest me how to show or hide a div based on the value of button.

Html code ##

<input type='button' value='Generate' onclick='f1(this)' />

Div section

<div id="sub_section" class="col s6" style="display:none">
 <TABLE STYLE="BORDER:2PX; BACKGROUND-COLOR:BLACK;">
 <TR>
 <td><label style="color:black;">TAMIL</label></td>
 </TR>
 </TABLE>   
 </div>
 </div>

jquery

function f1(objButton){  
var a=objButton.val;
   if(var a=='Generate')
    $("#sub_section").show();
}

jquery min.js

<script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
  • 写回答

5条回答 默认 最新

  • donglu8779 2017-11-29 07:18
    关注

    The following example will demonstrate you how to show and hide div elements based on the clicking of button. You can use toggle() function or show(), hide() function

    $(document).ready(function(){
        $('input[type="button"]').click(function(){
            var inputValue = $(this).attr("value");
            $("." + inputValue).toggle();
        });
    });
    .box{
       color: #fff;
       padding: 20px;
       display: none;
       margin-top: 20px;
    }
    .red, .redbtn{ background: #ff0000; }
    .green, .greenbtn{ background: #228B22; }
    .blue, .bluebtn{ background: #0000ff; }
    label{ margin-right: 15px; }
    .btn { 
       width: 100px;
       height: 30px;
       font-size: 20px;
       font-weight: 600;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
       <label><input type="button" class="btn redbtn" value="red" />
       <label><input type="button" class="btn greenbtn" value="green" />
       <label><input type="button" class="btn bluebtn" value="blue" />
    </div>
    <div class="red box">You have selected <strong>red button</strong> so i am here</div>
    <div class="green box">You have selected <strong>green button</strong> so i am here</div>
    <div class="blue box">You have selected <strong>blue button</strong> so i am here</div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?