Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从数据库检索值时未选中Jquery mobile复选框_Javascript_Jquery_Jquery Mobile_Checkbox_Cordova - Fatal编程技术网

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

Javascript 从数据库检索值时未选中Jquery mobile复选框,javascript,jquery,jquery-mobile,checkbox,cordova,Javascript,Jquery,Jquery Mobile,Checkbox,Cordova,在我的phonegap应用程序中,我使用jquery mobile。当从my DB中检索值时其他字段填写正确(文本框值填写正确)工作,复选框除外。我的问题是当DB返回true或false时无论它是什么,我的复选框中没有任何更改。我为这个问题挣扎了两天,有人建议解决这个问题的方法吗 //$('#checkbox1').prop('checked', true).checkboxradio('refresh'); //$("input[id='checkbox1']").attr("checked

在我的phonegap应用程序中,我使用
jquery mobile
。当
从my DB中检索值时
其他字段填写正确(
文本框值填写正确
)工作
,复选框除外
。我的问题是当
DB返回true或false时
无论它是什么,
我的复选框中没有任何更改
。我为这个问题挣扎了两天,有人建议解决这个问题的方法吗

//$('#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');
//$('#checkbox1').prop('checked', true).checkboxradio('refresh');
 //$("input[id='checkbox1']").attr("checked",results.rows.item(0).checkbox1).checkboxradio("refresh");
我也试过:

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

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

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

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

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

//$('#checkbox1').prop('checked', true).checkboxradio('refresh');
 //$("input[id='checkbox1']").attr("checked",results.rows.item(0).checkbox1).checkboxradio("refresh");
对于您的情况,方法是:

//$('#checkbox1').prop('checked', true).checkboxradio('refresh');
 //$("input[id='checkbox1']").attr("checked",results.rows.item(0).checkbox1).checkboxradio("refresh");
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);

你能在fiddle中重现这个问题吗?@Purus在放入fiddle时,onload无法工作fiddle@Purus看看这个,在使用cordova时,你应该把它放在DeviceRady事件中。addEventListener('deviceready',function(){},false);编辑我的答案如下。请尝试一下,当我点击复选框时,我需要点击两次以取消选中,它显示它已选中但不可见…只需使用上面的代码并删除您在body tag onload中添加的内容。问题是什么?我的回答有用吗?