简体   繁体   English

magnific-popup传递一个变量

[英]magnific-popup passing a variable

I'm trying to make magnific popup contain some forms. 我正在尝试制作包含某些形式的大胆弹出窗口 I would like to pass variables through the html anchor to the final popup. 我想通过html锚点将变量传递给最终的弹出窗口。 As Var1 in this pseudocode: 作为此伪代码中的Var1

<!-- HTML launcher element -->
<a href="#test-popup?Var1=X" class="open-popup-link">Show inline popup</a>

<!-- Popup itself -->
<div id="test-popup">
    <form method="post" action="">
        <input type="hidden" id="myVar" name="Var1" value="[placeholder]" />
        <input type="submit" />
    </form>
</div>

<!-- Inizialization -->
<script type="text/javascript">
    $(document).ready(function() {
        $('.open_popup_link').magnificPopup({
        type:'inline',
        midClick: true,

            function() {
                **Here some magic code that sets Var1 = X **
                $('#myVar').attr('value', function(i, attr){
                return attr.replace('[placeholder]', Var1);
                });}
        });
    });
</script>

I need this because I will generate serverside (w. PHP) the launchers so that each link will generate different form data. 我需要这个,因为我将生成服务器端(w.PHP)启动器,以便每个链接将生成不同的表单数据。

edit: One approach i thought at was to use custom attributes in the launcher, eg: 编辑:我想到的一种方法是在启动器中使用自定义属性,例如:

<a href="#test-popup" data-var1="X" class="open-popup-link">Show inline popup</a>

But I couldn't really get the right jQuery syntax for nesting the attributes processing INSIDE the magnific-popup inizialization. 但我无法真正获得正确的jQuery语法来嵌套属性处理INSIDE the magnific-popup inizialization。 My js/jquery knowledge is very basic, so thank you for any hint. 我的js / jquery知识非常基础,所以谢谢你的提示。

How about using a separate click handler outside the magnificPopup settings that changes the value of your input whenever a link is clicked, just make sure to bind it before initializing the plugin 如何在magnificPopup设置之外使用单独的单击处理程序,在单击链接时更改输入值,只需确保在初始化插件之前绑定它

HTML HTML

Using data attributes as correctly suggested 正确建议使用数据属性

<a href="#test-popup" data-var1="X" class="open-popup-link">Show inline popup</a>

Javascript 使用Javascript

$('.open-popup-link').click(function(){
    $('#myVar').val($(this).data('var1'));
});

$('.open_popup_link').magnificPopup({
   type:'inline',
   midClick: true
});

Try: 尝试:

Javascript/jQuery 使用Javascript / jQuery的

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

    $('.open_popup_link').magnificPopup({
    type:'inline',
    midClick: true,

        function() {
            **Here some magic code that sets Var1 = X **
             Var1 = $('#myVar').val(); 
            });}
    });
});

Not quite sure what you are using Var1 for, but that is how you would get the value from the input into a global Var1 variable. 不太确定你使用的是Var1 ,但是这就是你如何从输入中获取全局Var1变量的值。

Let me know if I didn't understand what you're trying to do correctly. 如果我不明白你正在尝试做什么,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM