scHintBox.html 5.6 KB

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