$(document).on('turbolinks:load', function(){ if ($('body.admins-competition-settings-index-page').length > 0) { var baseOptions = { autoclose: true, language: 'zh-CN', format: 'yyyy-mm-dd', startDate: '2017-04-01' }; var defineDateRangeSelect = function(element){ var options = $.extend({inputs: $(element).find('.start-date, .end-date')}, baseOptions); $(element).datepicker(options); $(element).find('.start-date').datepicker().on('changeDate', function(e){ $(element).find('.end-date').datepicker('setStartDate', e.date); }); }; defineDateRangeSelect('.teaching-mode-date'); defineDateRangeSelect('.competition-start-end-date'); var $basic_form = $('form.basic-setting-form'); $basic_form.validate({ errorElement: 'span', errorClass: 'danger text-danger', rules: { name: "required", subTitle: "required", startTime: "required", endTime: "required", mode: "required", identifier: "required" } }); // 保存按钮 $basic_form.on('click', ".submit-btn", function(){ $basic_form.find('.submit-btn').attr('disabled', 'disabled'); $basic_form.find('.error').html(''); var valid = $basic_form.valid(); $('input[name="navbar[][name]"]').each(function(_, e){ var $ele = $(e); if($ele.val() === undefined || $ele.val().length === 0){ $ele.addClass('danger text-danger'); valid = false; } else { $ele.removeClass('danger text-danger'); } }); if(!valid) return; $.ajax({ method: 'PATCH', dataType: 'json', url: $form.attr('action'), data: new FormData($form[0]), processData: false, contentType: false, success: function(data){ $.notify({ message: '保存成功' }); window.location.reload(); }, error: function(res){ var data = res.responseJSON; $form.find('.error').html(data.message); }, complete: function(){ $form.find('.submit-btn').attr('disabled', false); } }); }); });