将ID传递给Twitter Bootstrap模型

时间:2013-11-16 17:27:15

标签: javascript php jquery html twitter-bootstrap

我正在尝试将id传递给twitter bootstrap模型以进行删除确认(基本CRUD页面),并且对于我的生活,我无法使其工作。看了几个仍然无法上班的例子。我需要将data-id传递给模型并附加href链接,以便例如他们确认它将它们带到适当的页面(例如:delete.php?id = 5)任何想法都将非常感激。继承我的代码:

链接:

<a href="#msgDelete" data-toggle="modal" class="open-dialog btn btn-mini btn-danger" data-id="'.$row["id"].'">

型号:

<div id="msgDelete" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Are You Sure?</h3>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this product?  This action can not be reversed.  <br /><br /><em>Remember if you want to just hide the product from your store you can mark it as inactive.</em></p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<a href="delete.php?id=" class="btn btn-danger" id="clink" >Delete Product</a>
</div>

JavaScript:

<script type="text/javascript">

$(document).on("click", ".open-dialog", function () {
   var productId = $(this).data('id');
   console.log(productId);
   //$(".modal-body #clink").href( 'delete.php?id=' + productId );
   $("#clink").attr("href", "delete.php?idd=" + productId);
   // As pointed out in comments, 
   // it is superfluous to have to manually call the modal.
   // $('#addBookDialog').modal('show');
});

</script>

2 个答案:

答案 0 :(得分:0)

代码

<a href="#msgDelete" data-toggle="modal" class="open-dialog btn btn-mini btn-danger" data-id="'.$row["id"].'">

将其更改为

<a data-href="delete.php?id=<?php=productId?>" data-toggle="modal" class="open-dialog btn btn-mini btn-danger" data-id="'.$row["id"].'">

所以,它会自动传递给你的javascript ...而且你不需要在脚本中传递它...

将您的脚本更改为此

$(document).on("click", ".open-dialog", function () {
 var productId = $(this).data('id');
 console.log(productId);
});

答案 1 :(得分:0)

看起来你的代码印有错误。

$("#clink").attr("href", "delete.php?idd=" + productId);
                                  -----^

应该是

$("#clink").attr("href", "delete.php?id=" + productId); 

至少http://jsfiddle.net/egX3W/表示您根据需要改变了变化。