javascript - 根据复选框输入在 html 中分屏

标签 javascript jquery

我有四个复选框,单击其中任何一个都会从服务器返回各自的html代码。我希望在选择 2 个复选框时屏幕应分成 2 个,类似地在选择 3 个复选框时屏幕应分成 3 个。 如何使用 JqueryHTMLCSS 编写逻辑。

$(document).ready(function () {
    $("#go").click(function () {

        $("#output1").html('');
        $("#output2").html('');
        $("#output3").html('');
        $("#output4").html('');




        if ($('#getdisabledusers').prop("checked") == true)
        {
            $("#output1").html("<img src='http://blog.teamtreehouse.com/wp-content/uploads/2015/05/InternetSlowdown_Day.gif'  width='200' height='200' />");
            $("#box1").addClass("checkedbox");
            $.ajax({
                url: "/MVC_client/getdisabledusers",
                success: function (data) {
                    $("#output1").html(data);
                }
            });
        }

这里我有一个 JS 中的方法。 Output1、output2、output3.... 是四个 div。如果 checkbox 为 true ,则 ajax 调用将返回输出。

最佳答案

您可以使用 css display:tabledisplay:table-cell 做您想做的事情: DEMO

HTML

<div class="checkbox-container">
  <div class="input-box">
    <input type="checkbox"><label>option 1</label>
  </div>
  <div class="input-box">
    <input type="checkbox"><label>option 2</label>
  </div>
  <div class="input-box">
    <input type="checkbox"><label>option 3</label>
  </div>
  <div class="input-box">
    <input type="checkbox"><label>option 4</label>
  </div>
</div>
<div class="container">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</div>

SCSS

.container{
  width:100%;
  height:500px;
  display:table;

  section{
    display:none;
    &.show{
      display:table-cell;
    }
    &:nth-child(1){
      background-color:red;
    }
    &:nth-child(2){
      background-color:yellow;
    }
    &:nth-child(3){
      background-color:blue;
    }
    &:nth-child(4){
      background-color:green;
    }
  }
}
.checkbox-container{
  text-align:center;
  .input-box{
    display:inline-block;
  }
}

jQuery

$('input[type=checkbox]').change(function(){
    var index=$(this).parent().index();
    if($(this).is(':checked')){
        $('section').eq(index).addClass('show');
    //your ajax call goes here
  }
  else{
    $('section').eq(index).removeClass('show');
  }
});

关于javascript - 根据复选框输入在 html 中分屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523311/

相关文章:

javascript - 如何去掉 <br> 标签并用 <p> 和 </p> 标签换行?

javascript - jQuery:防止用户手指触摸元素时页面滚动

javascript - 如果未提交表单,则触发 onbeforeunload

javascript - 似乎无法将函数绑定(bind)到backbone.js 中查看

jquery - 如何使用 jQuery 确认点击链接

javascript - 跟踪按钮被点击的次数

javascript - 仅当所有字段都已填写时,如何接受值或启用按钮?

javascript - URL 中的多个井号

javascript - 在javascript block 中遍历twig数组

php - 脚本可以在本地主机上运行,​​但不能在服务器上运行