scHintBox.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <!----------------------------------------------------------------------------------------------------------------------
  2. chenshilong, 2018-04-29
  3. 自定义对话框,通用,用于替代系统对话框。系统自带的比较简陋,最严重的是一旦被浏览器屏蔽则无法再弹出。
  4. 自定义对话框包括:
  5. 1、只有一个按钮的信息提示框。
  6. 2、有两个按钮的操作确认询问框。
  7. 3、有三个按钮的多分支选择询问框。
  8. 4、输入文本值对话框。
  9. ----------------------------------------------------------------------------------------------------------------------->
  10. <div class="modal fade" id="hintBox_form" data-backdrop="static" style="z-index: 9999">
  11. <div class="modal-dialog" role="document">
  12. <div class="modal-content">
  13. <div class="modal-header">
  14. <h5 id="hintBox_title" class="modal-title">标题</h5>
  15. <button type="button" class="close" data-dismiss="modal" aria-label="Close" id="hintBox_btn_cross">
  16. <span aria-hidden="true">&times;</span>
  17. </button>
  18. </div>
  19. <div class="modal-body">
  20. <div id = "hintBox_caption" style="margin:5px 10px 10px 10px;">提示明细</div>
  21. <div style="margin:5px 10px 5px 10px;">
  22. <input id="hintBox_value" type="text" class="form-control" value="" />
  23. <p id="hintBox_error" style="margin-top:7px; color:red; display:none;">“xxx”已存在!</p>
  24. </div>
  25. </div>
  26. <div class="modal-footer" style="justify-content: center">
  27. <button type="button" class="btn btn-primary" data-dismiss="modal" id="hintBox_btn_yes">是</button>
  28. <button type="button" class="btn btn-primary" data-dismiss="modal" id="hintBox_btn_no">否</button>
  29. <button type="button" class="btn btn-secondary" data-dismiss="modal" id="hintBox_btn_cancel">取消</button>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <script>
  35. const hintBox = {
  36. value: null,
  37. btnType: {yes: 1, yesNo: 2, yesNoCancel: 3},
  38. defalultEvent: function () {
  39. $.bootstrapLoading.end();
  40. // alert('defalultEvent');
  41. return;
  42. },
  43. init: function (){
  44. // 事件类
  45. $("#hintBox_value").unbind();
  46. $("#hintBox_btn_yes").unbind();
  47. $('#hintBox_btn_no').unbind();
  48. $('#hintBox_btn_cancel').unbind();
  49. $("#hintBox_value").keyup(
  50. function (event) {
  51. hintBox.value = $('#hintBox_value').val();
  52. if (event.keyCode === 13) {
  53. $('#hintBox_btn_yes').click();
  54. return false;
  55. }
  56. }
  57. );
  58. $('#hintBox_btn_cancel').click(
  59. hintBox.defalultEvent
  60. );
  61. // 显示类
  62. $('#hintBox_caption').hide();
  63. $('#hintBox_value').hide();
  64. $('#hintBox_error').hide();
  65. $('#hintBox_btn_yes').hide();
  66. $('#hintBox_btn_no').hide();
  67. $('#hintBox_btn_cancel').hide();
  68. $('#hintBox_btn_cross').show();
  69. },
  70. font: function(str){
  71. return `<span style='color:red;font-weight:bold;font-size:15px'> ${str} </span>`;
  72. },
  73. error: function (err) { // 注意:该方法只能用在valueBox()的doOK回调函数中。
  74. $('#hintBox_error').text(err);
  75. $('#hintBox_error').show(200);
  76. $("#hintBox_value").focus();
  77. $("#hintBox_value").select();
  78. },
  79. infoBox: function (title, caption, btnType,
  80. doYes, doNo = hintBox.defalultEvent,
  81. btnTextArr = null, showCrossBtn = true) {
  82. this.init();
  83. if (!showCrossBtn)
  84. $('#hintBox_btn_cross').hide();
  85. $('#hintBox_title').text(title);
  86. $('#hintBox_caption').html(caption);
  87. $('#hintBox_caption').show();
  88. switch (btnType) {
  89. case this.btnType.yes:
  90. if (btnTextArr){
  91. $('#hintBox_btn_yes').text(btnTextArr[0]);
  92. }else
  93. $('#hintBox_btn_yes').text('确定');
  94. $('#hintBox_btn_yes').show();
  95. break;
  96. case this.btnType.yesNo:
  97. if (btnTextArr){
  98. $('#hintBox_btn_yes').text(btnTextArr[0]);
  99. $('#hintBox_btn_no').text(btnTextArr[1]);
  100. }else{
  101. $('#hintBox_btn_yes').text('是');
  102. $('#hintBox_btn_no').text('否');
  103. }
  104. $('#hintBox_btn_yes').show();
  105. $('#hintBox_btn_no').show();
  106. break;
  107. case this.btnType.yesNoCancel:
  108. if (btnTextArr){
  109. $('#hintBox_btn_yes').text(btnTextArr[0]);
  110. $('#hintBox_btn_no').text(btnTextArr[1]);
  111. $('#hintBox_btn_cancel').text(btnTextArr[2]);
  112. }else{
  113. $('#hintBox_btn_yes').text('是');
  114. $('#hintBox_btn_no').text('否');
  115. $('#hintBox_btn_cancel').text('取消');
  116. }
  117. $('#hintBox_btn_yes').show();
  118. $('#hintBox_btn_no').show();
  119. $('#hintBox_btn_cancel').show();
  120. break;
  121. }
  122. if (doYes){
  123. $('#hintBox_btn_yes').click(doYes);
  124. }
  125. if (doNo){
  126. $('#hintBox_btn_no').click(doNo);
  127. }
  128. $("#hintBox_form").modal('show');
  129. },
  130. valueBox: function (title, value, doOK) {
  131. this.init();
  132. $('#hintBox_title').text(title);
  133. this.value = value;
  134. $('#hintBox_value').show();
  135. $('#hintBox_value').val(value);
  136. $("#hintBox_value").focus();
  137. $("#hintBox_value").select();
  138. $('#hintBox_btn_yes').text('确定');
  139. $('#hintBox_btn_yes').show();
  140. $('#hintBox_btn_yes').click(doOK); // doOK不能给参数
  141. $('#hintBox_btn_no').text('取消');
  142. $('#hintBox_btn_no').show();
  143. $('#hintBox_btn_no').click(hintBox.defalultEvent);
  144. $("#hintBox_form").modal('show');
  145. }
  146. };
  147. $('#hintBox_form').on('hide.bs.modal', function() {
  148. $.bootstrapLoading.end();
  149. return;
  150. });
  151. </script>