dougu1896 2014-07-04 09:13
浏览 88

麻烦制作一个独特的按钮

I have a situation in which I use this java script for a single button and it works like this:

<script type="text/javascript">
$(document).ready(function(){

    $(document).on('click','a.btn-success',function(e){
        e.preventDefault();
        var myProductID=$(this).data('productid');
        var myUserid=$(this).data('iam');
        var postData={productID:myProductID,userid:myUserid};

        $.ajax({
            url: this.href,
            type:"POST",
            data: postData,
            success: function(data, textStatus, jqXHR){                
                $('a.btn-success').remove();
                $('a.btn-success').remove();
                $('#ajax-response').html('<p><button type="button" class="btn btn-info btn-xs btn-block">Added</button></p>');
            }
        });
    });
});
</script> 

And my link:

                    <!-- Add to cart button -->
                    <p><a class="btn btn-success btn-xs btn-block" data-productid="<?php echo $current_product->product_id; ?>" data-userid="<?php echo $userID; ?>" href="../incl/ajax-add-to-cart.php">Add</a></p>
                    <p id="ajax-response"></p>

And this works!!

What I now want is to use it with a list of products in which each product has besides its picture a button "add" which is represented by:

 a.btn-success

What I am doing is something like this:

              <!-- Add to cart button -->
                    <?php echo "<p><a class='btn btn-success'$button_id' btn-xs btn-block' data-productid=' $current_product->product_id;' data-userid='$userID' href='../incl/ajax-add-to-cart.php'>Add</a></p>"?>
                    <p id="ajax-response"></p>

Notice that I am trying to append a variable "$button_id" to the name of the button (it is really an an a tag with the style of a bootstrap button).

Then in the JavaScript I do as follows:

<script type="text/javascript">
$(document).ready(function(){

    $(document).on('click','a.btn-success'.$button_id,function(e){ //added button id
        e.preventDefault();
        var myBookID=$(this).data('bookid');
        var myUserid=$(this).data('iam');
        var postData={bookID:myBookID,userid:myUserid};

        $.ajax({
            url: this.href,
            type:"POST",
            data: postData,
            success: function(data, textStatus, jqXHR){                
                $('a.btn-success'.$button_id).remove();//added button id
                $('a.btn-success'.$button_id).remove();//added button id
                $('#ajax-response').html('<p><button type="button" class="btn btn-info btn-xs btn-block">Tillägt</button></p>');
            }
        });
    });
});
</script>

But it does not work. Can anybody help?

Thank you

  • 写回答

3条回答 默认 最新

  • dpdbu24262 2014-07-04 09:14
    关注

    You should probably change your function into:

    <script type="text/javascript">
    $(document).ready(function(){
        var button_id = '<?php echo $button_id; ?>';
        $(document).on('click','a.btn-success'+button_id,function(e){ //added button id
            e.preventDefault();
            var myBookID=$(this).data('bookid');
            var myUserid=$(this).data('iam');
            var postData={bookID:myBookID,userid:myUserid};
    
            $.ajax({
                url: this.href,
                type:"POST",
                data: postData,
                success: function(data, textStatus, jqXHR){                
                    $('a.btn-success'+button_id).remove();//added button id
                    $('a.btn-success'+button_id).remove();//added button id
                    $('#ajax-response').html('<p><button type="button" class="btn btn-info btn-xs btn-block">Tillägt</button></p>');
                }
            });
        });
    });
    </script>
    

    I assume this JavaScript file is inside PHP file where you can put $button_id value into JavaScript variable using simple <?php echo $button_id; ?>

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据