Bootstrap时钟选择器动态输入

时间:2016-02-28 17:34:44

标签: javascript jquery html5 twitter-bootstrap

参考下面的bootstrap代码我试图实现时钟选择器的动态输入,这里我使用了wrapper.append()来实现clockpicker文本框和其他2个文本框,点击一个按钮(添加)。

<div  style="height:70px;"></div>
    <div class="container-fluid" >
            <form role="form" name="frm" class="col-xs-12" action ="timeslot" method="post" 
            onSubmit="return valid2()">

            <div class="form-group">
                    <label for="name" >Slot Name</label>
                    <input type="text" name="slotname" class="form-control col-xs-6" placeholder="Time Slot name" >
             </div>
                <div  style="height:30px;"></div>
             <div class="form-group row">
             <div class="col-xs-6">
                    <label for="name" >Time Slot</label>
                    <input type="text" name="timeslot1" class="form-control col-xs-6" placeholder="Time Slot name" >
             </div> 
             <div class="col-xs-6">
                    <label for="name">Slot Available</label>
                    <input type="text" name="slotavailable1" class="form-control" placeholder="Slot available" >
             </div>
             </div>

             <label for="name" >Time</label>
            <div class=" form-group row">
                <div class="form-group clockpicker col-xs-6" data-placement="left" data-align="top" data-autoclose="true" >
                    <input type="text" id="start_time1" name="start_time1" class="form-control" value="09:30">
                    <span class="form-group-addon">
                        <span class="glyphicon glyphicon-time "></span>
                    </span>
                </div>

                <div class="form-group  clockpicker col-xs-6" data-placement="left" data-align="top" data-autoclose="true" >
                    <input type="text" id="end_time1" name="end_time1" class="form-control" value="09:30">
                    <span class="form-group-addon">
                        <span class="glyphicon glyphicon-time"></span>
                    </span>
                </div>
            </div>

            <div class="input_fields_wrap form-group">
                <button class="add_field_button btn btn-info">Add</button>
            </div>
            <div  style="height:30px;"></div>
            <div class="form-group">
                    <button type="submit" id="submit" class="btn btn-info" onkeyup="count(this.value)" name="submit">SUBMIT</button>
            </div>
        </div>
            </form>

            <script type="text/javascript">
            jQuery(function($) {
                $('.clockpicker').clockpicker({
                    placement: 'top',
                    align: 'left',
                    donetext: 'Done'
                });
        // The line below "closes" the document ready function
        });
            </script>
            <script>
            $(document).ready(function() {
                var max_fields      = 15; //maximum input boxes allowed
                var wrapper         = $(".input_fields_wrap"); //Fields wrapper
                var add_button      = $(".add_field_button"); //Add button ID

                var x = 2; //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
                        $(wrapper).append('<div> <div  style="height:30px;"></div>'+
                                '<div class="form-group row">'+
                        '<div class="col-xs-6">'+
                          '<label for="name"+x >Time Slot</label>'+
                          '<input type="text" name="timeslot"+x class="form-control col-xs-6" placeholder="Time Slot name" ></div>'+
                         '<div class="col-xs-6">'+
                            '<label for="name"+x>Slot Available</label>'+
                            '<input type="text" name="slotavailable"+x class="form-control" placeholder="Slot available" >'+
                         '</div>'+
                         '</div>'+

                         '<label for="name"+x >Time</label>'+
                         '<div class=" form-group row">'+
                         '<div class="form-group clockpicker col-xs-6" data-placement="left" data-align="top" data-autoclose="true" >'+
                             '<input type="text" id="start_time"+x name="start_time"+x class="form-control" value="09:30">'+
                             '<span class="form-group-addon">'+
                                '<span class="glyphicon glyphicon-time "></span>'+
                             '</span>'+
                            '</div>'+

                            '<div class="form-group  clockpicker col-xs-6" data-placement="left" data-align="top" data-autoclose="true" >'+
                                '<input type="text" id="end_time"+x name="end_time"+x class="form-control" value="09:30">'+
                                '<span class="form-group-addon">'+
                                    '<span class="glyphicon glyphicon-time"></span>'+
                                '</span>'+
                            '</div>'+
                            '</div>'+

                        '<a href="#" class="remove_field">Remove</a></div>'); //add input box
                          }
                });

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

ps:抱歉冗长的代码。 感谢@cale_b帮助我完成上一篇文章。

1 个答案:

答案 0 :(得分:2)

我将包含比原始问题更多的内容,只是为了提供帮助 - 因为有些事情可以让您的生活更轻松。

提示:您不需要单独的<script>块,也不需要单独的文档就绪功能。

解决方案:为了让您的timepicker能够处理动态添加的元素,您需要在动态添加它们后bind

最有条理的方法是创建一个绑定时钟选择器元素的函数,然后在需要时调用该函数:

// Script tags do not need type="text/javascript", so I've removed
<script>
jQuery(function($) {
    // The original "clockpicker" code is moved to the bottom in a function
    // No need to close and re-open script tags, so I've removed those
    // No need for a new "document ready" function, so I've removed that
    var max_fields      = 15; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 2; //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
            $(wrapper).append('<div> <div  style="height:30px;"></div>'+
                '<div class="form-group row">'+
                    '<div class="col-xs-6">'+
                      '<label for="name"+x >Time Slot</label>'+
                      '<input type="text" name="timeslot"+x class="form-control col-xs-6" placeholder="Time Slot name" ></div>'+
                     '<div class="col-xs-6">'+
                        '<label for="name"+x>Slot Available</label>'+
                        '<input type="text" name="slotavailable"+x class="form-control" placeholder="Slot available" >'+
                     '</div>'+
                '</div>'+
                '<label for="name"+x >Time</label>'+
                '<div class=" form-group row">'+
                    '<div class="form-group clockpicker col-xs-6" data-placement="left" data-align="top" data-autoclose="true" >'+
                        '<input type="text" id="start_time"+x name="start_time"+x class="form-control" value="09:30">'+
                         '<span class="form-group-addon">'+
                            '<span class="glyphicon glyphicon-time "></span>'+
                         '</span>'+
                    '</div>'+
                    '<div class="form-group  clockpicker col-xs-6" data-placement="left" data-align="top" data-autoclose="true" >'+
                        '<input type="text" id="end_time"+x name="end_time"+x class="form-control" value="09:30">'+
                        '<span class="form-group-addon">'+
                            '<span class="glyphicon glyphicon-time"></span>'+
                        '</span>'+
                    '</div>'+
                '</div>'+
            '<a href="#" class="remove_field">Remove</a></div>'); //add input box
            // NEW: Bind to the newly added clockpicker element
            bindClockPicker();
        }
    });

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

    // This function "binds" the clockpicker
    function bindClockPicker() {
        $('.clockpicker').clockpicker({
            placement: 'top',
            align: 'left',
            donetext: 'Done'
        });
    }

    // Last but not least, call this to bind to any existing clockpicker elements
    bindClockPicker();
});

</script>