簡體   English   中英

麻煩將php變量傳遞給模態窗口

[英]trouble passing php variable to modal window

嗨,我正在使用以下代碼將文件modal_window.php加載到當前頁面的模式窗口中。

<style>
        * {
            margin:0; 
            padding:0;
        }

        #overlay {
            position:fixed; 
            top:0;
            left:0;
            width:100%;
            height:100%;
            background:#000;
            opacity:0.5;
            filter:alpha(opacity=50);
        }

        #modal {
            position:absolute;
            background:url(tint20.png) 0 0 repeat;
            background:rgba(0,0,0,0.2);
            border-radius:14px;
            padding:8px;


        }

        #content {
            border-radius:8px;
            background:#fff;
                            padding:20px;

        }

        #close {
            position:absolute;
            background:url(close.png) 0 0 no-repeat;
            width:24px;
            height:27px;
            display:block;
            text-indent:-9999px;
            top:-7px;
            right:-7px;
        }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">  
            </script>
    <script>



        var modal = (function(){
            var 
            method = {},
            $overlay,
            $modal,
            $content,
            $close;

            // Center the modal in the viewport
            method.center = function () {
                var top, left;

                top = Math.max($(window).height() - $modal.outerHeight(), 0) 
                                     / 2;
                left = Math.max($(window).width() - $modal.outerWidth(), 0) 
                                   / 2;

                $modal.css({
                    top:top + $(window).scrollTop(), 
                    left:left + $(window).scrollLeft()
                });
            };

            // Open the modal
            method.open = function (settings) {
                $content.append(settings.content);

                $modal.css({
                    width: settings.width || 'auto', 
                    height: settings.height || 'auto'
                })

                method.center();

                $(window).bind('resize.modal', method.center);

                $modal.show();
                $overlay.show();
            };

            // Close the modal
            method.close = function () {
                $modal.hide();
                $overlay.hide();
                $content.empty();
                $(window).unbind('resize.modal');
            };

            // Generate the HTML and add it to the document
            $overlay = $('<div id="overlay"></div>');
            $modal = $('<div id="modal"></div>');
            $content = $('<div id="content"></div>');
            $close = $('<a id="close" href="#">close</a>');

            $modal.hide();
            $overlay.hide();
            $modal.append($content, $close);

            $(document).ready(function(){

                $('body').append($overlay, 
                            $modal);                        
            });

            $close.click(function(e){
                e.preventDefault();
                method.close();
            });

            return method;
        }());

        // Wait until the DOM has loaded before querying the document
        $(document).ready(function(){

                        varid = '<?php if(isset($_GET['id'])); ?>';


            $('a#testmodal').click(function(e){
                $.get('modal_window.php?id=varid', function(data){
                                      modal.open({content: data});});
                e.preventDefault();
            });





        });



    </script>
</head>
<body>


<a id="testmodal" href="modal.php?id=1">Test</a>

這樣做時,我嘗試使用以下方式將變量$ id傳遞給modal_window.php:

varid = '<?php if(isset($_GET['id'])); ?>';

接着

$('a#testmodal').click(function(e){
                $.get('modal_window.php?id=varid', function(data){
                                      modal.open({content: data});});
                e.preventDefault();
            });

而不是傳遞給modal_window.php的實際值$ id(在這種情況下為1),傳遞的是Java腳本變量(變量)的名稱。 因此,“ varid”就是modal_window.php顯示的內容。 有人看到我在犯錯誤嗎? 謝謝!

在您的get調用中,您擁有的ID等於文字變量。 相反,也許

   $.get('modal_window.php?id=' + varid, function(data){
                                  modal.open({content: data});});
            e.preventDefault();
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM