如何在模态操作URL中设置值

时间:2014-02-22 09:33:26

标签: javascript php jquery html twitter-bootstrap

单击删除按钮时,模态删除按钮链接具有删除按钮的值。 我的代码

<script>
$(document).on("click", "#myModal", function (e) {

    e.preventDefault();

    var _self = $(this);

    var myBookId = _self.data('adid');
    $("adid").val(myBookId);

    $(_self.attr('href')).modal('show');
});



</script>

删除按钮代码

print'<td><button class="btn btn-danger" data-toggle="modal" id="myModal" data-target="#myModal" data-adid='.$cd[$i][0].'>Delete Item</a></button>

模态代码

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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>
        <h4 class="modal-title" id="myModalLabel">Heads Up!
                     <p>What you are doing will delete a data!</p></h4>
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-success"><a href="a=yes&adid=" >Delete</a></button>
      </div>
    </div>
  </div>
</div>

点击删除项目按钮然后显示模态,此模态删除链接有一个网址,如?a=yes&adid=

&adid后有一个值,删除商品数据-adid。

但我的代码不起作用

1 个答案:

答案 0 :(得分:0)

首先,你有删除按钮和模态的相同ID(myModal)。 请尝试下面的代码

<button class="btn btn-danger" data-toggle="modal" id="deleteBtn" data-target="#myModal" data-adid='.$cd[$i][0].'>Delete Item</button>

<div class="modal hide" id="myModal"  tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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>
            <h4 class="modal-title" id="myModalLabel">Heads Up!
                         <p>What you are doing will delete a data!</p></h4>
          </div>

          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-success"><a href="" >adid=''</a></button>
          </div>
        </div>
      </div>
</div>

脚本

$(document).on("click", "#deleteBtn", function (e) {
    e.preventDefault();
    e.stopPropagation();
    var link = $(this).attr('data-adid');        
    $("#myModal .btn-success a").attr('href',link);
    $(".modal").modal("show");
});

JSFIDDLE