Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 复选框控件没有';无法接受页面加载时的值_Javascript_Jquery_Asp.net_Cookies_Checkbox - Fatal编程技术网

Javascript 复选框控件没有';无法接受页面加载时的值

Javascript 复选框控件没有';无法接受页面加载时的值,javascript,jquery,asp.net,cookies,checkbox,Javascript,Jquery,Asp.net,Cookies,Checkbox,我在我的_Layout.cshtml上有下面的JS。基本上,在每次“页面加载”中,复选框都被设置为Checked(true),即使代码应该将其设置为Cookie。 如果我只是在页面加载时将其设置为false,它就可以工作。(例如,$(“#ShowInactive”).prop(“checked”,false);)但是,正如您在代码中看到的,我可以根据Cookie在单击事件中设置复选框的值 我也尝试过让函数be$(document).ready(function(){)但是没有什么不同 <s

我在我的_Layout.cshtml上有下面的JS。基本上,在每次“页面加载”中,复选框都被设置为Checked(true),即使代码应该将其设置为Cookie。
如果我只是在页面加载时将其设置为false,它就可以工作。(例如,
$(“#ShowInactive”).prop(“checked”,false);
)但是,正如您在代码中看到的,我可以根据Cookie在单击事件中设置复选框的值

我也尝试过让函数be
$(document).ready(function(){
)但是没有什么不同

<script type="text/javascript">
    $(function () {
        console.log('1 - ' + Cookies.get('ShouldShowInactive'));
        var Inactive = Cookies.get('ShouldShowInactive') == true ? true : false;
        $("#ShowInactive").prop("checked", Inactive);

        $("#ShowInactive").click(function () {
            console.log('2 - ' + Cookies.get('ShouldShowInactive'));
            var ShowInactive = this.checked ? true : false;
            console.log('3 - ' + ShowInactive);
            Cookies.set('ShouldShowInactive', ShowInactive);
            console.log('4 - ' + Cookies.get('ShouldShowInactive'));
        });
    });
</script>

$(函数(){
console.log('1-'+Cookies.get('ShouldShowInactive');
var Inactive=Cookies.get('ShouldShowInactive')==true?true:false;
$(“#ShowInactive”).prop(“选中”,不活动);
$(“#显示不活动”)。单击(函数(){
console.log('2-'+Cookies.get('ShouldShowInactive');
var ShowInactive=this.checked?真:假;
console.log('3-'+ShowInactive);
Cookies.set('ShouldShowInactive',ShowInactive');
console.log('4-'+Cookies.get('ShouldShowInactive');
});
});
复选框控件:

<div class="pull-right checkbox">
   <label class="btn btn-success">
       @(Html.Kendo().CheckBox().Name("ShowInactive").Label("Show Inactive"))
   </label>
</div>

@(Html.Kendo().CheckBox().Name(“ShowInactive”).Label(“ShowInactive”))
当我通过
Console.log()
和Chrome开发工具检查cookie时,该值始终显示为
true
false
。此外,在使用上述方法之前,我尝试将其直接设置为
Cookies.get('ShouldShowInactive')
基本上是在页面加载时始终选中复选框的一种方式,而当前的方式总是不选中复选框。这两种方式都会忽略Cookie的正确值。

Cookies.get()函数返回一个字符串,
“true”==true
将计算为false。将条件更改为

 var Inactive = Cookies.get('ShouldShowInactive') == "true" ? true : false;

Cookies.get()函数是否返回字符串或布尔值?我认为这无关紧要,因为
非活动
变量在anyway=>
中设置为布尔值?true:false
@TCHdvlp
“true”==true
将返回false我相信它会返回字符串。我正在使用js-cookie()@tihom我会尝试将
==true
更改为
==“true”
@tihom哇,当你盯着某件事看太久,并说服自己奇怪的事情正在发生时,就会发生这种情况。你错过了显而易见的…谢谢。现在效果很好!