duanjuete9206 2016-06-07 08:26
浏览 85
已采纳

在HTML中显示$ .post的结果

I have a following HTML structure, which is a modal pop-up like following:

<!-- Left side of the pop up-->

     <div class="pop-state">
               <!-- state -->
               <?php foreach ($users as $user) { ?>
                   <div class="pop-first-btn">
                       <a class="check-user" value="<?=$user['id']?>"><?=$user['username']?></a>
                   </div>
               <?php  } ?>
           </div>

<!-- Right side of the modal popup-->
       <div class="pop-brands" >

           <?php for ($i = 0; $i < count($brandList); $i++) { ?>
               <div class="pop-city-co" id="brands">
                   <label>
                       <input class="check-city" type="checkbox" name="" value="<?=$brandList[$i]['id']?>" data-id="<?=$i ?>"><?=$brandList[$i]['name']?>
                   </label>
               </div>
           <?php  } ?>
       </div>

And when a user clicks on the a link I do the following jQuery event:

$('.check-user').click(function() {
    var user_id = $(this).attr('value');
    $.post("/user/usersBrands", {userId:user_id}, function(data){
        //console.log(data);
        var brands = $('<div />').append(data).find('#brands').html();
        $('#brands').html(brands);
        console.log(brands);
    });
});

I pick up the userId, send it to my function, and then I'd like to update the right side of my popup with the user list, in form of checkbox list... And this is my function that I'm calling:

 public function usersBrandsAction(){
        $userId = $this->getRequest()->getPost("userId");
        if(!empty($userId))
        {
            $brandList = $this->getBrandModel()->brandList();
            $this->dieCustomCode($brandList); // it can be echo instead of this or anything that would return me the array of brands...

        }
    }

So when I click on the a link, nothing shows up... the console says the #brands is undefined for some reason... Can someone help me out with this ? :/

  • 写回答

3条回答 默认 最新

  • dongwen7187 2016-06-07 10:53
    关注

    1) I suggest to use append() instead of html() until you really need to replace all the previous data with .html() method.

    So, instead of

    $('#brands').html(brands);

    use

    $('#brands').append(brands);

    Also, instead of:

    var brands = $('<div />').append(data).find('#brands').html();

    write:

    var brands = $('<div />').append(data);

    Because, once you append your data to the DIV element, you do not need traverse the DOM anymore in your case.

    2) Make sure, that you don't have duplication of elements with id "brands" in the DOM.

    3) Check if your "#brands" or ".pop-city-co" are not hidden with CSS.

    4) I also suggest to use json_encode on your PHP side and then parse it on client side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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