jQuery 将参数发送到 Bootstrap 模式窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24323895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:30:31  来源:igfitidea点击:

Send parameter to Bootstrap modal window?

jqueryhtmltwitter-bootstrapmodal-dialogwindow

提问by user3759455

I have a problem, I cannot pass any parameters to a modal window (using Bootstrap 3), I tried using the solution stated in this link, but I cannot make it work:

我有一个问题,我无法将任何参数传递给模式窗口(使用 Bootstrap 3),我尝试使用此链接中所述的解决方案,但无法使其工作:

Dynamically load information to Twitter Bootstrap modal

将信息动态加载到 Twitter Bootstrap 模态

(Solution kexxcream user).

(解决 kexxcream 用户)。

But pressing the button, the display does not show me anything, apparently blocks, attached a picture below:

但是按下按钮,显示器没有显示任何东西,显然是块,下面附上一张图片:

http://i.imgur.com/uzRUyf1.png

http://i.imgur.com/uzRUyf1.png

I have attached the code (same code post above left);

我附上了代码(左上方的相同代码帖子);

HTML Code:

HTML代码:

      <div id="myModal" class="modal hide fade">
      <div class="modal-header">
        <button class="close" data-dismiss="modal">&times;</button>
        <h3>Title</h3>
      </div>
      <div class="modal-body">            
          <div id="modalContent" style="display:none;">

          </div>
      </div>
      <div class="modal-footer">
        <a href="#" class="btn btn-info" data-dismiss="modal" >Close</a>
      </div>
  </div> 

JS Code:

JS代码:

    $("a[data-toggle=modal]").click(function() 
    {   
        var essay_id = $(this).attr('id');

        $.ajax({
            cache: false,
            type: 'POST',
            url: 'backend.php',
            data: 'EID='+essay_id,
            success: function(data) 
            {
                $('#myModal').show();
                $('#modalContent').show().html(data);
            }
        });
    });

Button:

按钮:

<a href='#myModal' data-toggle='modal' id='2'> Edit </a>

PHP Code (backend.php):

PHP 代码(后端.php):

<?php
$editId = $_POST['EID'];
?>
  <div class="jumbotron">
   <div class="container">
    <h1>The ID Selected is <?php echo $editId ?></h1>
   </div>
  </div>

回答by Joe

There is a better solution than the accepted answer, specifically using data-* attributes. Setting the id to 1 will cause you issues if any other element on the page has id=1. Instead, you can do:

有比接受的答案更好的解决方案,特别是使用 data-* 属性。如果页面上的任何其他元素的 id=1,将 id 设置为 1 会导致问题。相反,你可以这样做:

<button class="btn btn-primary" data-toggle="modal" data-target="#yourModalID" data-yourparameter="whateverYouWant">Load</button>

<script>
$('#yourModalID').on('show.bs.modal', function(e) {
  var yourparameter = e.relatedTarget.dataset.yourparameter;
  // Do some stuff w/ it.
});
</script>

回答by dfsq

First of all you should fix modal HTML structure. Now it's not correct, you don't need class .hide:

首先,您应该修复模态 HTML结构。现在不正确,您不需要 class .hide

<div id="edit-modal" class="modal fade" 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">Modal title</h4>
            </div>
            <div class="modal-body edit-content">
                ...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

Then links should point to this modal via data-targetattribute:

然后链接应该通过data-target属性指向这个模式:

<a href="#myModal" data-toggle="modal" id="1" data-target="#edit-modal">Edit 1</a>

Finally Js part becomes very simple:

最后js部分变得很简单:

    $('#edit-modal').on('show.bs.modal', function(e) {

        var $modal = $(this),
            esseyId = e.relatedTarget.id;

        $.ajax({
            cache: false,
            type: 'POST',
            url: 'backend.php',
            data: 'EID=' + essayId,
            success: function(data) {
                $modal.find('.edit-content').html(data);
            }
        });
    })

Demo: http://plnkr.co/edit/4XnLTZ557qMegqRmWVwU?p=preview

演示:http: //plnkr.co/edit/4XnLTZ557qMegqRmWVwU?p=preview

回答by latumi

I found the solution at: Passing data to a bootstrap modal

我在以下位置找到了解决方案: Passing data to a bootstrap modal

So simply use:

所以只需使用:

 $(e.relatedTarget).data('book-id'); 

with 'book-id' is a attribute of modal with pre-fix 'data-'

with ' book-id' 是带有前缀 ' data-'的模态属性

回答by Hugo Soto

I have found this better way , no need to remove data , just call the source of the remote content each time

我找到了这个更好的方法,不需要删除数据,每次只调用远程内容的源

$(document).ready(function() {
    $('.class').click(function() {
        var id = this.id;
        //alert(id);checking that have correct id
        $("#iframe").attr("src","url?id=" + id);
        $('#Modal').modal({ 
            show: true 
        });
    });
});