$(function(){ $(document).on('click', '#btn_newmessage', function(e){ $cmd = 'new'; $('input[name="cmd"]').val($cmd); submitForm('#form_add'); }); $(document).on('click', '#btn_editmessage', function(e){ $cmd = 'edit'; $msg = '確定儲存該筆資料?'; $form_id = '#form_add'; func_confirm($cmd, $msg, $form_id); }); $(document).on('click', '#btn_delmessage', function(e){ $cmd = 'del'; $msg = '一旦刪除後就無法回復!
您確定刪除該筆資料嗎?'; $form_id = '#form_add'; func_confirm($cmd, $msg, $form_id); }); $(document).on('keyup', '.acceptNumber', function(e){ //event.preventDefault(); //return acceptNumberStr(e.keyCode); this.value = this.value.replace(/[^0-9\.-]/g, ''); }); $(document).on('keyup', '.ValidateNumber', function(e, value){ return ValidateNumber(this,$(this).val()); }); $(document).on('keyup', '.ValidateFloat', function(e, value){ return ValidateFloat(this,$(this).val()); }); $(document).on('keyup', '.ValidateFloat_1', function(e, value){ return ValidateFloat_1(this,$(this).val()); }); $('input.switch').unbind('change').change(function(){ var $select = $(this).attr('data-select'); var $checked = $(this).prop('checked'); if($checked){ var $value = $(this).attr('data-true'); }else{ var $value = $(this).attr('data-false'); } $('select[name="'+$select+'"]').val($value).change(); }); //$('.datepicker').pickadate(); $('.datepicker').pickadate({ monthsFull: [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ], monthsShort: [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ], weekdaysFull: [ '星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六' ], weekdaysShort: [ '日', '一', '二', '三', '四', '五', '六' ], today: '今天', clear: '清除', close: '關閉', firstDay: 1, format: 'yyyy-mm-dd', formatSubmit: 'yyyy-mm-dd' }); $(document).on('click', '#func_confirm button[data-type="conform"]', function(){ var $form_id = $(this).attr('data-form_id'); if(typeof $form_id !== "undefined"&&$form_id!=''){ submitForm($form_id); } }); }); function ValidateNumber(e, pnumber) { if (!/^\d+$/.test(pnumber)) { var newValue =/^\d+/.exec(e.value); if (newValue != null) { e.value = newValue; } else { e.value = ""; } } return false; } function ValidateFloat(e, pnumber) { if (!/^\d+[.]?\d*$/.test(pnumber)) { var newValue = /^\d+[.]?\d*/.exec(e.value); if (newValue != null) { e.value = newValue; } else { e.value = ""; } } return false; } function ValidateFloat_1(e, pnumber) { if (!/^\d+[.]?[1-9]?$/.test(pnumber)) { var newValue = /\d+[.]?[1-9]?/.exec(e.value); if (newValue != null) { e.value = newValue; } else { e.value = ""; } } return false; } function func_alert($status, $msg, $redirect){ switch($status){ case 'ok': $img_url='./images/gw_dialog_success.png'; break; case 'fail': $img_url='./images/gw_dialog_stop.png'; break; case 'notice': $img_url='./images/gw_dialog_notice.png'; break; } func_close(); $('#window_msg').html($msg); $('#func_window').find('img').attr('src', $img_url); $('#func_window').on('hidden.bs.modal', function (e) { if($redirect == 'back'){ window.history.back(); }else if($redirect == 'reload'){ window.location.reload(); }else if($redirect == 'close'){ window.close(); }else if($redirect != ''){ window.location.replace($redirect); } }); $('#func_window').modal('show'); } function func_loading($msg){ console.log('loading'); $('#func_loading #loading_msg').html($msg); $('#func_loading').modal('show'); } function func_close(){ $('.modal').modal('hide'); } function func_confirm($cmd, $msg, $form_id){ $('input[name="cmd"]').val($cmd); $('#func_confirm #confirm_msg').html($msg); /*$('#func_confirm').on('shown.bs.modal', function (e) { console.log('bb'); $(this).find('button[data-type="conform"]').click(function(){ submitForm($form_id); }); });*/ $('#func_confirm button[data-type="conform"]').attr('data-form_id', $form_id); $('#func_confirm').modal('show'); } function submitForm(formid){ $.ajax({ url:'./?act=func', type:'POST', data:($(formid).serialize()), ajaxStart:func_loading(), success: function(response){ func_close(); try { console.log(response); JSON.parse(response); } catch(e) { console.log(response); } var obj = jQuery.parseJSON(response); func_alert(obj.status, obj.msg, obj.redirect); } }); }; function acceptNumberStr($code){ switch(true){ case ($code=='8'): case ($code=='9'): case ($code>='37'&&$code<='40'): case ($code=='46'): case ($code>='48'&&$code<='57'): case ($code>='96'&&$code<='105'): case ($code=='109'): case ($code=='110'): case ($code=='190'): return true; break; default: return false; break; } }