dongtiao5094 2016-12-31 05:51
浏览 35

点击jquery中的函数发生两次

HTML Section

<div class="right-part ap-search-8">
    <h5 class="insta-search-head"><?php echo \Application\Plugin\Common::z_xlt('Instant Search Filters'); ?>:</h5>
    <label class="tag alltags" title="<?php echo \Application\Plugin\Common::z_xlt('All Tags'); ?>"> 
      <input type="checkbox"> 
      <span class="tag-text"><?php echo \Application\Plugin\Common::z_xlt('All Tags'); ?></span>
    </label>
    <?php
     $default_filters = (empty($GLOBALS['zhemr']['default_patient_search_filters'])) ? array('fname|lname|mname','pd_pid'): (array)$GLOBALS['zhemr']['default_patient_search_filters']; ?>
    <label class="tag <?php if(in_array('fname|lname|mname',$default_filters)){ ?>ap-search-9<?php }?>" data-filter="fname|lname|mname" title="<?php echo \Application\Plugin\Common::z_xlt('Name'); ?>"> 
      <input type="checkbox"> 
      <span class="tag-text"><?php echo \Application\Plugin\Common::z_xlt('Name'); ?></span>
    </label>       
    <label class="tag <?php if(in_array('pd_pid',$default_filters)){ ?>ap-search-9<?php }?>" data-filter="pd_pid" title="<?php echo \Application\Plugin\Common::z_xlt('PID'); ?>"> 
      <input type="checkbox"> 
      <span class="tag-text"><?php echo \Application\Plugin\Common::z_xlt('PID'); ?></span>
    </label>
       <?php foreach($this->filters as $field_id => $filter) :
          ?>
          <label class="tag <?php if(in_array($field_id,$default_filters)){ ?>ap-search-9<?php }?>"   data-filter="<?php echo $field_id; ?>" title="<?php echo \Application\Plugin\Common::z_xlt($filter); ?>"> 
            <input type="checkbox"> 
            <span class="tag-text"><?php echo \Application\Plugin\Common::z_xlt($filter); ?></span>
          </label>
          <?php 
        endforeach;
      ?>     

  </div>

JS Section

$(".ap-search-8 .tag").click(function(ev) {
  if (!$(this).hasClass("alltags")) {
    ev.preventDefault();
    if ($(this).hasClass("ap-search-9")) {
      if ($(".ap-search-9").length < 2) return;
      $(this).removeClass("ap-search-9");
      $(this).removeClass("ap-search-adv");
      $(this).find("input[type='checkbox']").removeAttr('checked');
    } else {
      $(this).addClass("ap-search-9");
      $(this).addClass("ap-search-adv");
      $(this).find("input[type='checkbox']").attr('checked', "checked");
    }
  } else {
    if ($(this).find("input[type='checkbox']").is(":checked")) {
      //$(".ap-search-8 .tag").not(".alltags").addClass('ap-search-9');
      $(".ap-search-8 .tag").not(".alltags").addClass('ap-search-9').find("input[type='checkbox']").attr('checked', "checked");
    } else {
      //(".ap-search-8 .tag").not(".alltags").removeClass('ap-search-9');
      $(".ap-search-8 .tag").not(".alltags,[data-filter='fname|lname|mname'],[data-filter='pd_pid']").removeClass('ap-search-9').find("input[type='checkbox']").removeAttr('checked');
    }
  }
  var filter_fields = "";
  var i = 0;
  top.$(".ap-search-9").each(function() {
    i++;
    if (filter_fields != '') filter_fields += "****";
    filter_fields += $(this).attr("data-filter");
  });
  if ($('.ap-search-8').find('.ap-search-9').length == $('.ap-search-8').find('label.tag').not('.alltags').length) {
    $('.ap-search-8').find('.alltags').find("input[type='checkbox']").prop('checked', 'true');
  } else {
    $('.ap-search-8').find('.alltags').find("input[type='checkbox']").removeAttr('checked');
  }
  angular.element($('#angular_jquery_map')).scope().filterFields = filter_fields;
  requestRunning = true;
  memorizeSearchFilters();
  return false;
});

Here the click function happens twice for 'Alltags' label and also for other labels it works properly. If we put the preventDefault() function outside the if condition then the clicks become ok but the else part ie, click wrote for 'Alltags' doesnt work. The checkbox check and uncheck not happens. If anyone Knows the solution please help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大