jq设置checkbox默认选中状态 jQuery控制input不可编辑

1.后台发送的数据 是spring框架中的对象model.addObject(“student”,stu);
前台页面由隐藏的接收对象

页面代码:

<input id="wfsi" class="hidden" th:value="${student.age}"/>

<div>
<input id="ckbx" type="checkbox"/>
</div>
  • 1
  • 2
  • 3
  • 4
  • 5

jq代码设置checkbox的几种方式:

1.attr
//设置复选框为勾选状态
$("#ckbx").attr("checked","checked");

//设置复选框未选中状态
$("#ckbx").removeAttr("checked");

2.prop
//设置复选框为勾选状态
$("#ckbx").prop("checked",true);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

以上是在jq.1.12.4.js和bootstrap环境下运行,具体行为是前端html页面发送请求到controller,springBoot自动获取对应的数据进行处理,将返回的结果对象(Model以及addObject方法)传回给html,然后jq获取负责接收数据的input标签的值。

                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">2</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/qq_26917447">
                    <img src="https://profile.csdnimg.cn/D/1/E/3_qq_26917447" class="avatar_pic" username="qq_26917447">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/5.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_26917447" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">食火的埃尔德里奇</a></span>
                                            </div>
                    <div class="text"><span>发布了46 篇原创文章</span> · <span>获赞 8</span> · <span>访问量 7万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=qq_26917447" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    

    原文链接 https://blog.csdn.net/qq_26917447/article/details/78801392

    1.开启disabled,是input不可以编辑

      $("#id").attr("disabled","disabled");

    2.关闭disabled

      $("#id").removeAttr("disabled");

    普通js中是这样写的,document.getElementById("id").disabled = false;

    错误写法:$("#id").attr("disabled","false");

原文地址:https://www.cnblogs.com/sunny3158/p/12215488.html