Bootstrap驗證控制項的使用

來源:互聯網
上載者:User

標籤:

前端HTML代碼

<form id="myForm" method="post" class="form-horizontal" action="/Task/Test">                    <div class="modal-body">                                              <div class="form-group">                            <label class="col-lg-3 control-label">任務名稱</label>                            <div class="col-lg-5">                                <input type="text" class="form-control" name="takeName" id="takeName" />                            </div>                        </div>                        <div class="form-group">                            <label class="col-lg-3 control-label">程式集名稱</label>                            <div class="col-lg-5">                                <input type="text" class="form-control" name="dllName" id="dllName" />                            </div>                        </div>                        <div class="form-group">                            <label class="col-lg-3 control-label">類名稱</label>                            <div class="col-lg-5">                                <input type="text" class="form-control" name="methodName" id="methodName" />                            </div>                        </div>                        <div class="form-group">                            <label class="col-lg-3 control-label">cron表達</label>                            <div class="col-lg-5">                                <input type="text" class="form-control" name="cron" id="cron" />                            </div>                        </div>                        <div class="form-group">                            <label class="col-lg-3 control-label">運算式說明</label>                            <div class="col-lg-5">                                <input type="text" class="form-control" name="cronRemark" id="cronRemark" />                            </div>                        </div>                        <div class="form-group">                            <div class="col-lg-4 col-sm-4 col-xs-4">                                <div class="checkbox">                                    <label>                                        <input type="checkbox" class="colored-success" checked="checked" id="enabled">                                        <span class="text">啟用</span>                                    </label>                                </div>                            </div>                        </div>                        </div>                    <div class="modal-footer">                        <button type="button" class="btn btn-default"                                data-dismiss="modal">                            關閉                        </button>                        <button type="submit" class="btn btn-primary"> 提交</button>                    </div>                </form>
View Code

JS

<script>        $(document).ready(function () {            $("#myForm").bootstrapValidator({                message: ‘This value is not valid‘,                feedbackIcons: {                    valid: ‘glyphicon glyphicon-ok‘,                    invalid: ‘glyphicon glyphicon-remove‘,                    validating: ‘glyphicon glyphicon-refresh‘                },                fields: {                    takeName: {                        validators: {                            notEmpty: {                                message: ‘任務名稱不可為空‘                            }                        }                    },                    dllName: {                        validators: {                            notEmpty: {                                message: ‘程式集名稱不可為空‘                            },                            //remote: {//ajax驗證。server result:{"valid",true or false} 向服務發送當前input name值,獲得一個json資料。例表示正確:{"valid",true}                              //    url: ‘/Task/Test3‘,//驗證地址                            //    message: ‘使用者已存在‘,//提示訊息                            //    delay :3000,                            //    type: ‘POST‘,//請求方式                                                            //    /**自訂提交資料,預設值提交當前input value                            //     *  data: function(validator) {                            //          return {                            //              password: $(‘[name="passwordNameAttributeInYourForm"]‘).val(),                            //              whatever: $(‘[name="whateverNameAttributeInYourForm"]‘).val()                            //          };                            //       }                            //     */                            //},                        }                    },                    methodName: {                        validators: {                            notEmpty: {                                message: ‘類名稱不可為空‘                            }                        }                    },                    cron: {                        validators: {                            notEmpty: {                                message: ‘cron表達不可為空‘                            }                        }                    }                },                submitHandler: function (validator, form, submitButton) {                    var taskData = {};                    taskData.taskName = $(‘#takeName‘).val();                    taskData.dllPath = $(‘#dllName‘).val();                    taskData.methodName = $(‘#methodName‘).val();                    taskData.cronExpression = $(‘#cron‘).val();                    taskData.remark = $(‘#cronRemark‘).val();                    taskData.enabled = $(‘#enabled‘).is(‘:checked‘);                    $.ajax({                        type: "post",                        url: "/Task/AddTask",                        data:taskData,                        success: function (data) {                            alert(data);                            $(‘#myForm‘).data(‘bootstrapValidator‘).resetForm(true);                        }                    });                }            })        })    </script>
View Code

該方式為AJAX提交,提交事件寫在submitHandler

Bootstrap驗證控制項的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.