【问题标题】:how to make drop down checkbox selected even i reload the page using Jquery and angular 4?即使我使用 Jquery 和 Angular 4 重新加载页面,如何选择下拉复选框?
【发布时间】:2018-07-16 03:08:06
【问题描述】:

我正在使用带有集成 JQuery 的 Angular 4,一旦选择了下拉字段,则包含下拉复选框的表单不应更改,即使我重新加载页面或直到我重新选择。

dropdown.html:

<div class="chkdropdown">
          Show and Hide Columns
        <ul>
             <li>
               <input type="checkbox" class="hidecol" value="name" id="col_2" />&nbsp;S NO&nbsp;                
            </li>
            <li>
                <input type="checkbox" class="hidecol" value="name" id="col_3" />&nbsp;DrugName&nbsp;                
           </li>
           <li>
              <input type="checkbox" class="hidecol" value="salary" id="col_4" />&nbsp; Generic Name&nbsp;                  
           </li>
           <li>
              <input type="checkbox" class="hidecol" value="gender" id="col_5" />&nbsp;Generic Combination&nbsp;                    
            </li>
            <li>
                <input type="checkbox" class="hidecol" value="city" id="col_6" />&nbsp;Dosage &nbsp;              
              </li>
              <li>
                  <input type="checkbox" class="hidecol" value="email" id="col_7" />&nbsp;VAT &nbsp;               
                </li>
        </ul>
    </div>

ang.component.ts:

$(document).ready(function(){                       
            $(".hidecol").click(function(){

                var id = this.id;
                var splitid = id.split("_");
                var colno = splitid[1];
                var checked = true;                                        
                if($(this).is(":checked")){
                    checked = true;
                }else{
                    checked = false;
                }
                setTimeout(function(){
                    if(checked){
                        $('#drug_table td:nth-child('+colno+')').hide();
                        $('#drug_table th:nth-child('+colno+')').hide();
                    } else{
                        $('#drug_table td:nth-child('+colno+')').show();
                        $('#drug_table th:nth-child('+colno+')').show();
                    }

                }, 500);

            });
        });

【问题讨论】:

    标签: jquery angular checkbox jquery-plugins


    【解决方案1】:

    在你的 jquery 中添加这段代码

        $(document).ready(function(){
    //here i checked localstorage values
        var checkedvalues = JSON.parse(localStorage.getItem("checkedvalues"));
        console.log(checkedvalues);
    //created obj to store check values
        var someObj={};
        someObj.checked=[];
    //looped through checkvalues from local storge ad add checked property to its values
        $.each(checkedvalues, function(key, value) {
          console.log(key,value);
          $("#" + value).prop('checked', value);
          console.log($("#" + value));
        });
                    $(".hidecol").click(function(){
    
                        var id = this.id;
                        var splitid = id.split("_");
                        var colno = splitid[1];
                        var checked = true;                                        
                        if($(this).is(":checked")){
                            checked = true;
    //push chekced values in above created array.
                            someObj.checked.push($(this).attr("id"));
                            console.log(someObj.checked);
    //add same values in local storage.
                            localStorage.setItem("checkedvalues", JSON.stringify(someObj.checked));
    
                        }else{
                            checked = false;
                        }
                        setTimeout(function(){
                            if(checked){
                                $('#drug_table td:nth-child('+colno+')').hide();
                                $('#drug_table th:nth-child('+colno+')').hide();
                            } else{
                                $('#drug_table td:nth-child('+colno+')').show();
                                $('#drug_table th:nth-child('+colno+')').show();
                            }
    
                        }, 500);
    
                    });
                });
    

    这在 html 中

    <div class="chkdropdown">
              Show and Hide Columns
            <ul>
                 <li>
                   <input type="checkbox" class="hidecol" value="name" id="col_2" />&nbsp;S NO&nbsp;                
                </li>
                <li>
                    <input type="checkbox" class="hidecol" value="name" id="col_3" />&nbsp;DrugName&nbsp;                
               </li>
               <li>
                  <input type="checkbox" class="hidecol" value="salary" id="col_4" />&nbsp; Generic Name&nbsp;                  
               </li>
               <li>
                  <input type="checkbox" class="hidecol" value="gender" id="col_5" />&nbsp;Generic Combination&nbsp;                    
                </li>
                <li>
                    <input type="checkbox" class="hidecol" value="city" id="col_6" />&nbsp;Dosage &nbsp;              
                  </li>
                  <li>
                      <input type="checkbox" class="hidecol" value="email" id="col_7" />&nbsp;VAT &nbsp;               
                    </li>
            </ul>
        </div>
    

    【讨论】:

    • 我按照你上面给出的那样做了,但没有任何变化!!我将此代码应用于 ng onint jquery 函数..
    • 检查本地存储浏览器中的值是否正确设置
    • 按照我分享的教程链接,它应该可以按预期工作并且很容易集成。
    • 我有一个包含下拉复选框的表单,一旦选择了下拉字段,即使我重新加载页面或重新选择框,也不应该更改它......
    • 很简单,您是否获得了从复选框中选择的值?如果是,只需在该行下方添加代码以在本地存储中设置该值。当页面重新加载或在 document.reload 上时,您从本地存储中调用该本地存储值。值应该是 {"option1":true,"option2":false,"option3":false}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2021-10-19
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多