【问题标题】:Jquery mobile checkbox not checked when value is retrieved from DB从数据库中检索值时未选中 Jquery 移动复选框
【发布时间】:2013-11-29 01:50:31
【问题描述】:

在我的 phonegap 应用程序中,我使用 jquery mobile。当retrieve the value from my DB 正确填写其他字段时(textbox values are filled correctly)工作except checkbox。我的问题是当DB returns true or falseno 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');

【问题讨论】:

  • 你能在小提琴中重现这个问题吗?
  • @Purus 放入小提琴时,onload 在小提琴中不起作用
  • @Purus 看看这个jsfiddle.net/aravinth/DtY4V
  • 您应该在使用cordova 时将其置于deviceready 事件中。 document.addEventListener('deviceready', function() {}, false);
  • 在下面编辑了我的回答。请尝试一下

标签: javascript jquery jquery-mobile checkbox cordova


【解决方案1】:

试试这个:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 当我单击复选框时,我需要单击两次才能取消选中它显示它已选中但不可见....
    • 只要上面的代码,去掉你在body标签onload中添加的内容。
    • 出了什么问题?我的回答有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多