drbouzlxb92333332 2017-05-22 16:48
浏览 21

too long

i have read all the stackoverflow solutions but it didnt work.

i have some divs and when i click on them it should change and load different data from server via ajax. but whenever i click on it the page scroll to top. i have tried "return false" and preventDefault . here is my code:

$('.part').click(function(e) {
    var _this = $(this);
    if ($(this).attr('data-selected') == "") {
        _this.html('');
        var id = 76;
        $.post('banners/bannerjs.php', {
            'id': id
        }, function(result) {
            $(_this).append(result);
            $(_this).attr('data-selected', 'selected');

        });
    } else if ($(this).attr('data-selected') == "selected") {
        alert('salam');
    }
    return false;
});

and here is my html code:

<div class="part type<?php echo $w['TypeID']?>" style="" data-price="<?php echo $w['Price']?>" data-id="<?php echo $w['id']?>"
              data-selected="<?php echo $selected?>" data-banner-selected="<?php echo $w['Display_Banner_Selected']?>"
              data-banner="<?php echo $w['Display_Banner']?>">
              <script type="text/javascript">
              //jQuery.ajaxSetup({async:false});
                if($('[data-id="<?php echo $w['id']?>"]').attr('data-selected') == "selected"){
                  var id = <?php echo $w['Display_Banner_Selected']?>;
                }else{
                var id = <?php echo $w['Display_Banner']?>;
              }
                $.post('banners/bannerjs.php', {'id': id}, function(result) {
                  $('[data-id="<?php echo $w['id']?>"]').append(result);
                });
                $('.<?php echo $banner['name'].$banner['id']?>').append('<div class="partPrice"><?php echo $w["Price"]?></div>');

              </script>

            <br>
          </div>
  • 写回答

2条回答 默认 最新

  • dtukyb8095 2017-05-22 16:52
    关注

    Updated code:

    <script
      src="https://code.jquery.com/jquery-1.12.4.min.js"
      integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
      crossorigin="anonymous"></script>
    <script>
    $(function(){
      $(".part").click(function(e) {
        e.preventDefault(); //this disables the default behavior of the element
          var _this = $(this);
          if ($(this).attr('data-selected') == "") {
              _this.html('');
              var id = 76;
              $.post('testy.php', {
                  'id': id
              }, function(result) {
                  $(_this).append(result);
                  $(_this).attr('data-selected', 'selected');
    
              });
          } else if ($(this).attr('data-selected') == "selected") {
              alert('salam');
          }
          return false;
      });
    })
    </script>
    <a class="part" data-selected="">Foo</a>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度