1

我面临一个 iCheck 插件的奇怪问题。我很少有带有单选按钮和复选框的字段。我正在使用 iCheck 插件来美化它们。除了动态复选框和收音机外,一切正常。

当我动态添加几个单选按钮并立即调用 iCheck 函数时,什么都没有发生。但是,如果我从 firebug 调用相同的函数,它的工作和 iCheck 功能就会与无线电相关联。它出什么问题了。

/* Here my code for adding dynamic radios with class red */
$('input[type="checkbox"].red, input[type="radio"].red').iCheck({
    checkboxClass: 'icheckbox_minimal-red',
    radioClass: 'iradio_minimal-red',
    increaseArea: '10%' // optional
});
alert(1);

警报正在执行,所以我相信我的 iCheck 代码也很好。但奇怪的是,如果我从 firebug 执行上述 iCheck 代码,收音机就会与 iCheck 功能绑定。我不明白这个问题。

另一个奇怪的事情是,当我添加另一组收音机时,之前动态创建的一组收音机与 iCheck 绑定,而新组仍然没有绑定。因此,当添加新的集合时,之前的集合会与 iCheck 绑定

实际代码

 $('#btn_savefield').click(function () {
            var fID, m, opt, cap;
            fID = $('#<%= dd_fieldtype.ClientID %>').val();
            var fe = $('#hid_fieldedit').val();
            m = $('#cb_mandatoryfield').prop('checked');
            if (m == false) {
                m = 0;
            }
            else {
                m = 1;
            }

            var s = $('#txt_fieldoptions').val();
            opt = s.replace(/\r\n|\r|\n/g, '|');
            cap = $('#txt_fieldcaption').val();

            var tempParam, Param;
            var tempParam = { FieldTypeID: fID, fieldTitle: cap, newoptions: opt, mandatory: m };
            var param = JSON.stringify(tempParam);
            //alert(fID);
            //alert(param);
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: param,
                url: "postjobs.aspx/getFieldType",
                dataType: "json",
                error: function (data) {
                    //alert("error");
                    //alert(data);
                    alert(JSON.stringify(data));
                },
                success: function (data) {
                    var f = data.d;
                    //alert(f);
                    if (data.d != '') {
                        if (parseInt(fe) >= 0) {
                            $('#<%= div_formfields.ClientID %>').find('.form-group:eq(' + fe.toString() + ')').replaceWith(f);
                        }
                        else {

                            $('#<%= div_formfields.ClientID %> hr').before(data.d);
                        }
                    }

                }
            });
            $('#Modal_newfield').modal('hide');
            alert($('input[type="checkbox"].red, input[type="radio"].red').length);
            $('input[type="checkbox"].red, input[type="radio"].red').iCheck({
                checkboxClass: 'icheckbox_minimal-red',
                radioClass: 'iradio_minimal-red',
                increaseArea: '10%' // optional
            });

        });
4

1 回答 1

2

AJAX 是异步的,使用成功回调:

  success: function (data) {
     var f = data.d;
     //alert(f);
     if (data.d != '') {
         if (parseInt(fe) >= 0) {
             $('#<%= div_formfields.ClientID %>').find('.form-group:eq(' + fe.toString() + ')').replaceWith(f);
         } else {

             $('#<%= div_formfields.ClientID %> hr').before(data.d);
         }
     }

     alert($('input[type="checkbox"].red, input[type="radio"].red').length);
     $('input[type="checkbox"].red, input[type="radio"].red').iCheck({
         checkboxClass: 'icheckbox_minimal-red',
         radioClass: 'iradio_minimal-red',
         increaseArea: '10%' // optional
     });

 }
于 2014-03-04T09:59:07.190 回答