You need to enable JavaScript to run this app. jQuery .prop方法失效,页面加载时无法选中复选框如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

jQuery .prop方法失效,页面加载时无法选中复选框如何解决?

问题原因
  • 语法错误:代码中误将jQuery对象包装写法$(this)写为${this},${}是ES6模板字符串的插值语法,此处用法完全错误,会直接抛出未定义变量的异常,导致后续代码无法执行。
  • 方法依赖错误:checkboxradio("refresh")是jQuery Mobile框架专属的组件刷新方法,如果你没有正确引入jQuery Mobile依赖,调用该不存在的方法会触发JS异常,中断逻辑执行。就算你确实使用了jQuery Mobile,前面的语法错误也会导致方法无法被正确调用。
解决方案

情况1:仅使用原生jQuery,未引入jQuery Mobile

直接删掉checkboxradio("refresh")调用,同时修正${this}为$(this)即可,修改后的代码如下:

$(document).ready(function(){
  $('.check').each(function() {
    $(this).prop('checked',true);
    console.log($(this).attr("value"));
  });
});

如果使用的是1.6以下的旧版jQuery,可以搭配attr写法兼容:$(this).attr('checked','checked'),1.6及以上版本推荐优先使用prop操作表单原生属性。

情况2:确实使用jQuery Mobile框架

首先确认已经正确引入jQuery和jQuery Mobile的依赖文件,再修正语法错误即可,修改后的代码如下:

$(document).ready(function(){
  $('.check').each(function() {
    $(this).prop('checked',true).checkboxradio("refresh");
    console.log($(this).attr("value"));
  });
});

内容的提问来源于stack exchange,提问作者Keerthi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 09:00:00