当前位置:   article > 正文

Jquery两个插件的搭配使用: Select2 和 popover验证_popover + select2

popover + select2

1、使用的插件:

(1) select2-3.5.2/select2.min.js

(2) jquery.validate.bootstrap.popover.js、jquery.validate.min.js

 

2、应用环境

首先看一下设计需求(见下图):

页面中“Select one or more branch”处可以多选,使用的是select2插件,这里真正的select被隐藏,并且需要验证(当且仅当By Branch地单选框被选中,且未选择branch时,给出错误提示)。而右侧CCM Permissions下的Manage CCM的复选框下也存在隐藏输入框,并且只在显示时需要验证。如下图

3、编码分析(后台php,框架CI3)

关键HTML代码如下:

  1. <div class="form-group">
  2. <?php echo form_label('Location Assignment', '', array('class' => 'control-label')); ?>
  3. <div class="control-label">
  4. <label class="radio-inline">
  5. <input type="radio" name="sub_type" value="0" checked>
  6. To all locations
  7. </label>
  8. <label class="radio-inline">
  9. <input type="radio" name="sub_type" id="sub_type" value="1">
  10. By Branch
  11. </label>
  12. </div>
  13. </div>
  14. <div id= "sub_branch" class="form-group hide">
  15. <?php echo form_label('Branches', '', array('class'=>'control-label'));?>
  16. <select id="branches" name="branches[]" multiple="multiple" style="width: 100%" >
  17. <?php foreach ($branches as $branch): ?>
  18. <option value="<?php echo $branch->id; ?>"><?php echo $branch->branch_name; ?></option>
  19. <?php endforeach ?>
  20. </select>
  21. </div>

jQuery代码设计:

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $('#branches').select2({
  4. placeholder: "Select one or more branch",
  5. });
  6. var sub_type = $('input[name=sub_type]:checked').val();
  7. if (sub_type == 1) {
  8. $('#sub_branch').removeClass('hide');
  9. } else {
  10. $('#sub_branch').removeClass('hide').addClass('hide');
  11. }
  12. $('input[name=sub_type]').on('change', function(event) {
  13. if(this.value == 1) {
  14. $('#sub_branch').removeClass('hide');
  15. } else {
  16. $('#sub_branch').removeClass('hide').addClass('hide');
  17. }
  18. });
  19. jQuery.validator.addMethod("required2", function(value, element){
  20. return !$("#sub_type").is(":checked") || ($(element).val() ? true : false);
  21. });
  22. $("#form-create-user").validate_popover({
  23. rules: {
  24. "branches[]": { //多选select,使用branches[],并且用双引号""括起来。
  25. required2: true,
  26. }
  27. },
  28. messages: {
  29. "branches[]": {
  30. required2: "<?php echo lang('location_branch_required'); ?>",
  31. }
  32. },
  33. popoverPosition: 'top',
  34. ignore: ".ignore", //仅对.ignore类的对象忽略验证,对隐藏对象进行验证
  35. hideForInvisible: false,
  36. get_offset_element: function(element) {//重新定位隐藏对象的位置
  37. if ($(element).is(":hidden")) {
  38. return $(element).prev('div');
  39. } else {
  40. return $(element);
  41. }
  42. },
  43. submitHandler: function(form) {
  44. $('#btn-create-user').attr('disabled', 'disabled');
  45. $('#form-create-user').prepend('<div id="processing-msg" class="alert alert-info"><?php echo lang("message_processing"); ?></div>');
  46. form.submit();
  47. }
  48. });
  49. });
  50. </script>

4、注意问题

在使用对select2下拉框进行required验证时,还要注意下拉框的缺省值不要写成0=>'xxx',而要写成
 

$branch_options = array('' => '--SELECT--');
 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/412621
推荐阅读
相关标签
  

闽ICP备14008679号