在剃须刀页面中,如何保留不属于表格的复选框的状态?

发布于 2025-02-08 06:28:24 字数 206 浏览 2 评论 0原文

我的页面具有用于展开页面某些部分的复选框。这是在JavaScript中完成的客户端逻辑。

我想保留此特定页面的此复选框的状态。剃刀页可以自动执行此操作吗? 我尝试通过bool属性 [bindproperty(supportsget = true)] 在pageModel中然后重新加载(HTTP GET)复选框始终是错误的。

I have page that has checkbox that is used to expand/collapse some part of the page. This is client-side logic done in JavaScript.

I want to preserve the state of this checkbox for this particular page. Can Razor Pages do this automatically?
I tried by adding bool property with [BindProperty(SupportsGet = true)] in PageModel but it doesn't work - when I check the checkbox and reload (HTTP GET) the checkbox is always false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2025-02-15 06:28:24

猜测此切换功能是特定于用户的,并且您想在许多HTTP请求上坚持他们的选择,我建议使用客户端代码设置cookie,该代码是用户或更准确的设备 - 特定的,并且可以在需要的情况下持续使用,并且也可以在服务器上阅读。

Guessing that this toggle feature is user-specific, and that you want to persist their choice over a number of HTTP requests, I recommend setting a cookie using client-side code, which is user- or more accurately device-specific and can persist for as long as you need, and can be read on the server too.

二货你真萌 2025-02-15 06:28:24

我想保留此特定页面此复选框的状态。剃须刀可以自动执行此操作吗?

不,由于您不将其发送到后端,因此不会显示它。

正如迈克所说,最好将其存储在客户端饼干或存储中。

更多详细信息,您可以参考以下代码:

<p>
  <input type="checkbox" id="cbox1" checked="checked">
  <label >This is the first checkbox</label>
</p>
 

@section scripts{
    <script>

        $(function(){
            var status = getValue();
                if(status === "true"){
                   $("#cbox1").attr("checked","checked");
                }else{
                  $("#cbox1").removeAttr("checked");
                }
        })

        $("#cbox1").click(function(){
            var re =  $("#cbox1").is(":checked")
            alert(re);
           createItem(re);
        
        });

        function createItem(value) {
                localStorage.setItem('status', value); 
            } 
 
            function getValue() {
                return localStorage.getItem('status');  
            } // Gets the value of 'nameOfItem' and returns it
            console.log(getValue()); //'value';
    </script>

}

I want to preserve the state of this checkbox for this particular page. Can Razor Pages do this automatically?

No, since you don't send it to the backend it will not show it.

As Mike said, it better we could store it inside the client cookie or storage.

More details, you could refer to below codes:

<p>
  <input type="checkbox" id="cbox1" checked="checked">
  <label >This is the first checkbox</label>
</p>
 

@section scripts{
    <script>

        $(function(){
            var status = getValue();
                if(status === "true"){
                   $("#cbox1").attr("checked","checked");
                }else{
                  $("#cbox1").removeAttr("checked");
                }
        })

        $("#cbox1").click(function(){
            var re =  $("#cbox1").is(":checked")
            alert(re);
           createItem(re);
        
        });

        function createItem(value) {
                localStorage.setItem('status', value); 
            } 
 
            function getValue() {
                return localStorage.getItem('status');  
            } // Gets the value of 'nameOfItem' and returns it
            console.log(getValue()); //'value';
    </script>

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文