从DB检索值时,不检查Jquery移动复选框

时间:2013-11-28 13:57:00

标签: javascript jquery jquery-mobile checkbox cordova

在我的phonegap应用程序中,我使用jquery mobile。正确填写retrieve the value from my DB其他字段时textbox values are filled correctlyexcept 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');

2 个答案:

答案 0 :(得分:0)

试试这个:

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

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

答案 1 :(得分:0)

一旦将值动态设置为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);