Bootstrap switch 開關事件處理

    需要使用Bootstrap switch,實現通過、拒絕功能並且在開關至拒絕時,顯示textarea框輸入原因。

    1、css引用
    <link href="switch/css/bootstrap-switch.min.css" rel="stylesheet">

    2、js引用
    <script src="switch/js/bootstrap-switch.min.js"></script>

    3、頁面(使用modal打開,若須使用需引用對應的js和css)

     <div class="modal inmodal" id="approve_modal" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog">
                    <div class="modal-content animated bounceInRight">
                            <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal">
                                    <span aria-hidden="true">&times;</span><span class="sr-only">關閉</span>
                                    </button>
                                    <h1 class="modal-title">月 報 審 批</h1>
                                    <br>
                                    <input type="checkbox" name="my-checkbox" id="my-checkbox">
                            </div>
                            <div class="modal-body">
                                    <div class="form-group animated fadeIn" id="approve_input" style="display:none;">
                                            <label style="font-size: 15px;">若不通過,請說明原因:</label> 
                                            <textarea id="approve_area" rows="5" placeholder="填寫內容請控制在250字以內。" class="form-control" maxlength="250">
                                            </textarea>
                                    </div>
                            </div>
                            <div class="modal-footer">
                                    <button type="button" class="btn btn-white" data-dismiss="modal">關閉</button>
                                    <button type="button" class="btn btn-primary" id="submit">提交</button>
                            </div>
                    </div>
            </div>
    </div>

    4、js頁面

    $("[name='my-checkbox']").bootstrapSwitch({
        onText : "拒絕",
        offText : "通過",
        onColor : "danger",
        offColor : "success",
        size : "large",

                    //onSwitchChange方法監測switch開關狀態

        onSwitchChange : function() {

                    //.prop方法查看input的checked屬性,即使查看其開關狀態

            var checkedOfAll=$("#my-checkbox").prop("checked");

                            //false、true對應input的checked屬性,顯示和隱藏輸入框

            if (checkedOfAll==false){
                $('#approve_input').hide()
            }
            else {
                $('#approve_input').show()
            }
        }
        });
    $('#submit').click(function () {
        var checkedOfAll=$("#my-checkbox").prop("checked");
        var checkinfo=$('#approve_area').val();
        alert(checkedOfAll+checkinfo)
    })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章