打开模态并将其添加到其他div时淡化li

时间:2015-01-06 11:17:37

标签: javascript jquery html bootstrap-modal

当我点击产品时,它打开了模态并添加到我的产品列表中,但是如果将特定产品添加到“我的产品”列表中,我希望该特定产品褪色,并且当重新打开模式时,我也希望该按钮变得褪色,以便它无法再次添加到列表中。

<script type="text/javascript"> <!--This is the code what i'm able to do so far-->
$(document).ready(function() {
 $('button[id^="allttm"]').click(function(e) {
e.preventDefault();
 var $this = $(this);
  var data_id = $this.attr('data-id');
$("#mylist").append('<li class="has-item">' + data_id + '</li>');
 });
});
</script>

<ul class="unstyled" id="products">
<li class="dummycontent" id="itemdetail" data-id="6425809">
    <a class="dummyclass" href="javascript:void(0)" id="" data-toggle="modal" data-target="#basicModal">
<span class="itemname">product 1</span>
</a>
</li>
</ul>
<h3 id="myproductlistheader">My productlist</h3>
<ol id="mylist" class="productlist">
</ol>

这个模态区域:

<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
 <div class="modal-content">
 <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  </div>
 <div class="modal-body">
<div class="procuctifo">
  <div>
  <h3 class="productheading">product 1</h3>
</div>
</div>
   </div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" data-dismiss="modal" id="allttm_1" data-id="product 1">Add to list</button>
 </div>
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

您可以在产品项目添加到列表后添加另一个类,并根据产品是否包含该类,您可以将其添加到列表中。您的代码可能如下所示:

$('.products').click(function(){
    if($(this).hasClass('added')){
        //product in the list , no need to add

    }else{
        // add class and add product to list
        $(this).addClass('added');
        //add product to list
    }
});

希望有所帮助