如果选中复选框,Jquery不起作用?

时间:2014-12-30 08:53:47

标签: javascript jquery html css checkbox

当用户选中其中一个复选框时,我尝试使用每个复选框的进度条进行选择,进度条将显示/显示。所以,我已经将进度条的css编码为display:none;并设置了复选框,如果选中$('.pbar').css('display','block');,但这不起作用,任何人都可以帮我查看我的代码吗?我犯了什么错感谢

JS Fiddle

HTML

<div id="popupfoot">
<p id="survey_title"></p>
 <h5 id="choose"></h5>

<div id="survey_question"></div>
</div>

JS

var PG = {
    divid: "",
    multiselection: "",
    optionitem: [],
    init: function (divid, multiselection, optionitem) {
        PG.divid = divid;
        PG.multiselect = multiselection;
        PG.optionitem = optionitem;

    },
    test: function () {
        for (var i = 0; PG.optionitem.length > i; i++) {
            alert(PG.optionitem[i].name);
        }
    },

    render_1: function () {
        $.each(array, function (i, obj) {

            var selection = "<input class='the_checkbox' type='checkbox' id=" + obj.value + " name=" + obj.name + " value=" + obj.value + ">" +
                "<label class='label' for=" + obj.value + ">" + obj.value + "</label>" +
                "<div class='pbar'><div class='pbarinner' style='width:75%;'></div></div>";

            $("#" + PG.divid).append(selection);
                    if ($('input#'+obj.value).is(':checked')) {
                        $('.pbar').css('display','block');
                    }


        });



        $("#survey_title").append("What is your favorite fruit??");
        $("#choose").append("Please select 1 fruit only:");

        $('.the_checkbox').on('change', function (evt) {
            if ($(this).siblings(':checked').length >= PG.multiselect) {
                this.checked = false;
            }
        });



    },
    render_2: function () {
        $.each(w_array, function (i, obj) {
            var selection = "<input class='the_checkbox' type='checkbox' id=" + obj.value + " name=" + obj.name + " value=" + obj.value + ">" +
                "<label class='label' for=" + obj.value + ">" + obj.value + "</label>";


            $("#" + PG.divid).append(selection);

        });

        $("#survey_title").append("item??");
        $("#choose").append("Please select 3 item :");

        $('.the_checkbox').on('change', function (evt) {
            if ($(this).siblings(':checked').length >= PG.multiselect) {
                this.checked = false;
            }
        });



    },
    save: function () {}
}


var array = [];
array[0] = {
    "name": "fruit",
    "value": "mango"
};
array[1] = {
    "name": "fruit",
    "value": "apple"
};
array[2] = {
    "name": "fruit",
    "value": "orange"
};
array[3] = {
    "name": "fruit",
    "value": "banana"
};


var w_array = [];
w_array[0] = {
    "name": "com",
    "value": "RAM"
};
w_array[1] = {
    "name": "com",
    "value": "DISK"
};
w_array[2] = {
    "name": "com",
    "value": "BOOK"
};
w_array[3] = {
    "name": "com",
    "value": "PEN"
};


PG.init("popupfoot", "1", array);
PG.render_1();
/*PG.init("survey_question", "3", w_array);
PG.render_2();*/

1 个答案:

答案 0 :(得分:0)

您必须为复选框添加侦听器才能显示进度条

$('input#'+obj.value).change(
     function(){
        if (this.checked) {
            $('.pbar').css('display','block');
            /*Rest of your code*/
        }
        else
        {
            $('.pbar').css('display','none');
        }

});

以下是更新后的JSFiddle