javascript - 从数据库检索值时未选中 Jquery mobile 复选框

标签 javascript jquery jquery-mobile checkbox cordova

在我的phonegap应用程序中,我使用jquery mobile。当从我的数据库检索值时,其他字段都已正确填写(文本框值已正确填写),除了复选框之外。我的问题是,当数据库返回 true 或 false 时,无论它是什么,我的复选框都没有变化。我为这个问题苦苦挣扎了两天,有人建议解决这个问题的解决方案..

我的编码是这样的:

 if (results.rows.item(0).checkbox1 == true) 
     $("#checkbox1").attr('checked', 'checked');
 else $("#checkbox1").removeAttr('checked');

也尝试过这个:

//$('#checkbox1').prop('checked', true).checkboxradio('refresh');
 //$("input[id='checkbox1']").attr("checked",results.rows.item(0).checkbox1).checkboxradio("refresh");

已解决: 喜欢

if (results.rows.item(0).checkbox1 === 'true') 
     $("#checkbox1").attr('checked', 'checked');
 else $("#checkbox1").removeAttr('checked');

最佳答案

试试这个:

var dbValue = (results.rows.item(0).checkbox1 === true);
$('#checkbox1').prop('checked', dbValue);

注意第一行中的===。如果您的数据库返回值类似于 1 或字符串“true”,请明确检查它。

关于javascript - 从数据库检索值时未选中 Jquery mobile 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20268041/

相关文章:

ruby-on-rails - 无法上传图像(回形针)和 jquery mobile

javascript - TypeScript 中的 querySelectorAll 等效项

javascript - 如何使用 jquery 将 URL 字符串分成几部分并插入到变量中

javascript - 通过ajax从select框获取值到mysql

javascript - jQuery .data() 值中包含空格的问题

javascript - Datatables 1.10 添加编辑和删除按钮列

jquery - PhoneGap、Bootstrap 还是 JQuery Mobile?

jquery - PhoneGap + jQuery Mobile + cakePHP

javascript - 为什么 JavaScript ES6 不支持多构造类?

javascript - LeanModal 中的关闭按钮