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



在我的phonegap应用程序中,我使用jquery mobile。当retrieve the value from my DB时,其他字段被正确填充(textbox values are filled correctly)工作except checkbox。我的问题是当DB returns true or false不管它是什么都是no changes in my checkbox。我在这个问题上挣扎了两天,有人提出解决这个问题的解决方案。。

我的编码是这样的:

 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);

注意第一行中的===。如果DB返回值类似于1或字符串"true",请明确检查它。

在jQuery Mobile中动态设置表单元素的值后,必须进行刷新。

对于您的情况,方法是:

document.addEventListener('deviceready', function() {
    var a= true;
    alert("hi");
    if(a == true) {
       $("#checkbox1").attr("checked",true);
       $("#checkbox2").attr("checked",true);
    } else {
      $("#checkbox1").removeAttr('checked');
      $("#checkbox2").removeAttr('checked');
    }
    $("#checkbox1").attr("checked",true).checkboxradio("refresh");
}, false);

最新更新