【问题标题】:put a check on a checkbox if its values are in database如果复选框的值在数据库中,请选中它
【发布时间】:2014-08-12 08:38:19
【问题描述】:

我有这些 checkboxes 我已经可以从它的表中获取值,但我只是不知道它会如何检查那些带有已插入我的数据库中的值的复选框。

 function editPageLink(id){  
    $.ajax({
        type    : 'GET',
        async   : false,  
        data    : {
            key : id
        },
        url     : baseUrl+'/admin/getdiscountinfo',
        error: function(req,error){ 
                    notify('e',req.statusText);
                },
        dataType: 'json',
        cache: false,
        success : function(msg){    
            if(Object.keys(msg).length != 0){ 
                newPage();
                saveType = 'u';
                key = msg[0].key;
                $('#discountCode').val(msg[0].discountCode);
                $('#discountName').val(msg[0].discountName);
                $('#discountDesc').val(msg[0].discountDesc);
                $('#discountType').val(msg[0].discountType);
                $('#expiration').val(msg[0].expiration);
                $('#expStartDate').val(msg[0].expStartDate);
                $('#expEndDate').val(msg[0].expEndDate);
                $('#discountValue').val(msg[0].discountValue);
                $('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
                    $('.chkbox2').check(checked); // i dunno if this is right.
                };
                $('#discounteTransaction').val(msg[0].discountTransaction);
            }
        }
    }); 

}

我试过了,但还是不行。感谢您的帮助! :D

编辑:我的#productId'.chkbox2' 是一样的。是这部分,取值对吗?

 $('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
    $('.chkbox2').check(checked);
 };

【问题讨论】:

    标签: php jquery mysql phalcon


    【解决方案1】:
    $('.chkbox2').prop('checked', true); 
    

    使用它来检查复选框

    【讨论】:

      【解决方案2】:
      $('.chkbox2').prop('checked', true);
      

      以上代码会将复选框设置为选中

      $('.chkbox2').prop('checked', false);
      

      以上代码将取消勾选复选框

      【讨论】:

        【解决方案3】:
        $('.chkbox2').check(checked); // i dunno if this is right.
        

        在 jQuery 中检查复选框的标准方法是使用 .prop():

        $('.chkbox2').prop('checked', bool);
        

        如果bool = true,复选框将被选中。 (或者,如果bool 为假,它将被取消选中)

        您也可以通过省略第二个参数来检索当前的检查状态:

        $('.chkbox2').prop('checked'); // Returns true or false.
        

        另外,在你的 if 条件下,你有一个错误:

        if ('.chkbox2'.val() == true){
        

        应该是

        if( $('.chkbox2').val() == true )
        

        因为您缺少选择器的 jQuery 包装器。

        【讨论】:

        • 你能测试以确保你的成功回调正在执行并且从它返回的数据是有效的吗?此外,您的条件为“如果此复选框有值,请执行此操作..”是否正确?
        • ahm err.. "If this check box has a value, do this.." 实际上是那个条件,我检查了我的复选框值是否已经插入到我的数据库中。我不知道那个条件是否正确。对不起先生
        【解决方案4】:

        啊!最后。我解决了我只是改变了

        $('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
                        $('.chkbox2').check(checked); // i dunno if this is right.
                    };
        

        $('.chkbox2').val(msg[0].productId,':checked',true);
        

        哎呀哎呀!哈哈感谢您的 cmets 我获得了想法:D

        【讨论】:

          【解决方案5】:

          尝试将此添加到您的代码中:D

          $('.chkbox2').val(msg[0].productId,':checked',true);
          

          【讨论】:

            【解决方案6】:

            $('#checkbox').attr('checked')

            $('#checkbox').prop('checked') // 真

            $('#checkbox').is(':checked') // 真

            $('#checkbox')[0].checked // true

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-06-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多