scHintBox.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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">
  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. init: function (){
  39. // 事件类
  40. $("#hintBox_value").unbind();
  41. $("#hintBox_btn_yes").unbind();
  42. $('#hintBox_btn_no').unbind();
  43. $('#hintBox_btn_cancel').unbind();
  44. $("#hintBox_value").keyup(
  45. function (event) {
  46. hintBox.value = $('#hintBox_value').val();
  47. if (event.keyCode === 13) {
  48. $('#hintBox_btn_yes').click();
  49. return false;
  50. }
  51. }
  52. );
  53. $('#hintBox_btn_cancel').click(
  54. function () {
  55. $.bootstrapLoading.end();
  56. return;
  57. }
  58. );
  59. // 显示类
  60. $('#hintBox_caption').hide();
  61. $('#hintBox_value').hide();
  62. $('#hintBox_error').hide();
  63. $('#hintBox_btn_yes').hide();
  64. $('#hintBox_btn_no').hide();
  65. $('#hintBox_btn_cancel').hide();
  66. },
  67. font: function(str){
  68. return `<span style='color:red;font-weight:bold;font-size:15px'> ${str} </span>`;
  69. },
  70. error: function (err) { // 注意:该方法只能用在valueBox()的doOK回调函数中。
  71. $('#hintBox_error').text(err);
  72. $('#hintBox_error').show(200);
  73. $("#hintBox_value").focus();
  74. $("#hintBox_value").select();
  75. },
  76. infoBox: function (title, caption, btnType, doYes, doNo, btnTextArr) {
  77. this.init();
  78. $('#hintBox_title').text(title);
  79. $('#hintBox_caption').html(caption);
  80. $('#hintBox_caption').show();
  81. switch (btnType) {
  82. case this.btnType.yes:
  83. if (btnTextArr){
  84. $('#hintBox_btn_yes').text(btnTextArr[0]);
  85. }else
  86. $('#hintBox_btn_yes').text('确定');
  87. $('#hintBox_btn_yes').show();
  88. break;
  89. case this.btnType.yesNo:
  90. if (btnTextArr){
  91. $('#hintBox_btn_yes').text(btnTextArr[0]);
  92. $('#hintBox_btn_no').text(btnTextArr[1]);
  93. }else{
  94. $('#hintBox_btn_yes').text('是');
  95. $('#hintBox_btn_no').text('否');
  96. }
  97. $('#hintBox_btn_yes').show();
  98. $('#hintBox_btn_no').show();
  99. break;
  100. case this.btnType.yesNoCancel:
  101. if (btnTextArr){
  102. $('#hintBox_btn_yes').text(btnTextArr[0]);
  103. $('#hintBox_btn_no').text(btnTextArr[1]);
  104. $('#hintBox_btn_cancel').text(btnTextArr[2]);
  105. }else{
  106. $('#hintBox_btn_yes').text('是');
  107. $('#hintBox_btn_no').text('否');
  108. $('#hintBox_btn_cancel').text('取消');
  109. }
  110. $('#hintBox_btn_yes').show();
  111. $('#hintBox_btn_no').show();
  112. $('#hintBox_btn_cancel').show();
  113. break;
  114. }
  115. if (doYes){
  116. $('#hintBox_btn_yes').click(doYes);
  117. }
  118. if (doNo){
  119. $('#hintBox_btn_no').click(doNo);
  120. }
  121. $("#hintBox_form").modal('show');
  122. },
  123. valueBox: function (title, value, doOK) {
  124. this.init();
  125. $('#hintBox_title').text(title);
  126. this.value = value;
  127. $('#hintBox_value').show();
  128. $('#hintBox_value').val(value);
  129. $("#hintBox_value").focus();
  130. $("#hintBox_value").select();
  131. $('#hintBox_btn_yes').text('确定');
  132. $('#hintBox_btn_yes').show();
  133. $('#hintBox_btn_yes').click(doOK); // doOK不能给参数
  134. $("#hintBox_form").modal('show');
  135. }
  136. };
  137. $('#hintBox_form').on('hide.bs.modal', function() {
  138. $.bootstrapLoading.end();
  139. return;
  140. });
  141. </script>