简体   繁体   English

隐藏内容后如何刷新div

[英]How to refresh a div after hiding contents

I have the following code:我有以下代码:

 // I am filling the data of the "MultiSelectDialog_List" div using javascript like this: var s = ''; // JSON string ----- YOU NEED TO ADD AN EXAMPLE OF S var jsonData = JSON.parse(s); for (var i = 0; i < jsonData.length; i++) { // Hold the original list $("#MultiSelectDialog_List").append("<input type='checkbox' id ='" + jsonData[i][idProp] + "' value='" + jsonData[i][idProp] + "' data-value='" + jsonData[i][nameProp].toLowerCase() + "' > <label data-value ='" + jsonData[i][nameProp].toLowerCase() + "' id ='lbl" + jsonData[i][idProp] + "'>" + jsonData[i][nameProp] + "</label> <br/>"); } // Then I am using this script to hide some content: var enteredText = $("#MultiSelectDialog_Search").val(); var ary = $("input[type='checkbox']:not([data-value*='" + enteredText.toLowerCase() + "'])"); for (var i = 0; i < ary.length; i++) { $("#" + ary[i]["id"]).hide(); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="multiSelectDialog" title="Select"> <input type="text" id="MultiSelectDialog_Search" name="MultiSelectDialog_Search" onchange="multiSelectDialog_Search_TextChanged()" /> <div id="MultiSelectDialog_List" data-value=""> <!--The data will goes here--> </div> <input type="hidden" name="MultiSelectDialog_Values" id="MultiSelectDialog_Values" /> <button id="MultiSelectDialog_Submit" onclick="multiSelectDialog_SubmitButton_Click()">Submit</button> </div>

The problem is that the content of the div are not redrawen correctly after hiding some elements.问题是隐藏了一些元素后,div的内容没有正确重绘。 Check the image:检查图像:

Before hiding elements隐藏元素之前

When hiding elements, see the gaps隐藏元素时,查看间隙

How should I refresh the div?我应该如何刷新div? I tried to use hide() and show(), fadein().我尝试使用 hide() 和 show()、fadein()。 but did not work.但没有用。

Try something like this尝试这样的事情

$(document).ready(function(){
    // use localStorage.removeItem('show'); to unset an item
    var show = localStorage.getItem('show');
    if(show === 'true'){
        $('#MultiSelectDialog_List').show();
    }
    
    $("#btn").click(function(event){
        event.preventDefault();
        $('#MultiSelectDialog_List').show();
        localStorage.setItem('show', 'true');
    });
});

 // I am filling the data of the "MultiSelectDialog_List" div using javascript like this: var s = '[{"EnName":"Device Linked Officer","ArName":"Device Linked Officer","Status":false,"Project":"CMS","ID":"ROL29","IsOrderNumRequired":true,"IsTimeInterval":false,"IsHealthCare":true,"isBiller":false},{"EnName":"pharmacist","ArName":"pharmacist","Status":false,"Project":"CMS","ID":"ROL30","IsOrderNumRequired":true,"IsTimeInterval":false,"IsHealthCare":true,"isBiller":false},{"EnName":"Store & Pharmacy","ArName":"Store & Pharmacy","Status":false,"Project":"CMS"}]'; // JSON string ----- YOU NEED TO ADD AN EXAMPLE OF S var jsonData = JSON.parse(s); var idProp = 'ID'; var nameProp = 'EnName'; for (var i = 0; i < jsonData.length; i++) { // Hold the original list $("#MultiSelectDialog_List").append("<input type='checkbox' id ='" + jsonData[i][idProp] + "' value='" + jsonData[i][idProp] + "' data-value='" + jsonData[i][nameProp].toLowerCase() + "' > <label data-value ='" + jsonData[i][nameProp].toLowerCase() + "' id ='lbl" + jsonData[i][idProp] + "'>" + jsonData[i][nameProp] + "</label> <br/>"); } // Then I am using this script to hide some content: function multiSelectDialog_SubmitButton_Click() { var enteredText = $("#MultiSelectDialog_Search").val(); var ary = $("input[type='checkbox']:not([data-value*='" + enteredText.toLowerCase() + "'])"); for (var i = 0; i < ary.length; i++) { $("#" + ary[i]["id"]).hide(); } } function multiSelectDialog_Search_TextChanged() { // TODO }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="multiSelectDialog" title="Select"> <input type="text" id="MultiSelectDialog_Search" name="MultiSelectDialog_Search" onchange="multiSelectDialog_Search_TextChanged()" /> <div id="MultiSelectDialog_List" data-value=""> <!--The data will goes here--> </div> <input type="hidden" name="MultiSelectDialog_Values" id="MultiSelectDialog_Values" /> <button id="MultiSelectDialog_Submit" onclick="multiSelectDialog_SubmitButton_Click()">Submit</button> </div>

I've updated the given snippet a little bit to try to achieve a working code.我已经稍微更新了给定的代码片段以尝试实现工作代码。

Your problem is, that its not really clear what idProp or nameProp is (so I decided on my own).你的问题是,它并不清楚idPropnameProp是什么(所以我自己决定)。

Another problem you will face:您将面临的另一个问题:

You do hide stuff, but you never get it back.你确实隐藏了东西,但你永远不会找回它。 Once a checkbox got hidden its gone for good.一旦复选框被隐藏,它就永远消失了。

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

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