jquery - 根据文档宽度折叠面板

时间:2015-12-14 20:29:54

标签: javascript jquery

如果文档宽度小于,我想缩小面板。 514.我所使用的脚本不起作用:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<?php
$num = 0;
echo '
<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="'.${'myText'.$num.}.'"></div>
</div>
';
?>

<script type="text/javascript">
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            //${myText.$num}
            // I don't know how to add this without php code to keep changing every added input box.
            $(wrapper).append('<div><input type="text" name="myText"/><a href="#" class="remove_field">Remove</a></div>').clone(true); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>

以及正在运行的脚本,允许用户手动折叠/展开面板:

    jQuery(document).ready(function($){

    if ($(document).width() < 514) {

            if (!$('.panel-heading').hasClass('panel-collapsed')) {
            // collapse the panel
                $(this).parents('.panel').find('.panel-body').slideUp();
                $(this).addClass('panel-collapsed');
                $(this).find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
            }

    };
});

和HTML:

jQuery(function ($) {
    $('.panel-heading span.clickable').on("click", function (e) {
        if ($(this).hasClass('panel-collapsed')) {
            // expand the panel
            $(this).parents('.panel').find('.panel-body').slideDown();
            $(this).removeClass('panel-collapsed');
            $(this).find('i').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
        }
        else {
            // collapse the panel
            $(this).parents('.panel').find('.panel-body').slideUp();
            $(this).addClass('panel-collapsed');
            $(this).find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
        }
    });
});

谢谢, 贾斯汀。

1 个答案:

答案 0 :(得分:2)

您应该替换dao.create(…)事件所使用的选择器的null参考。

this

});