简体   繁体   English

多个模态Windows尝试在JS中使用正则表达式

[英]Multiple Modal Windows trying to use regular expressions in JS

guys I'm not very familiar with Javascript and the usage of regular expressions I've spent hours digging through resources and trying multiple different ways and just can't seem to get it to work. 伙计们,我对Javascript和正则表达式的使用不太熟悉,我花了数小时来研究资源并尝试多种不同方式,但似乎无法使其正常工作。

I have a menu where when you click the links a modal window will pop up. 我有一个菜单,当您单击链接时,将弹出一个模式窗口。 Currently I have 18 of these links on a page - so there is 18 different modal windows. 目前,我在页面上有18个这些链接-因此有18个不同的模态窗口。 I have each labeled in the php file like so: 我每个人都在php文件中贴上了这样的标签:

<div class="button_menu">
    <a href="#dialog0" name="modal0" /><div class="button">HQ</div></a>
</div>

Then at the bottom of the PHP File I have this. 然后在PHP文件的底部,我有这个。

<div id="dialog0">
     (Omitted)
</div>
<div id="mask"></div>

This will repeat 18 times from dialog0 - 17 and modal0 - 17. 这将从dialog0-17和modal0-17重复18次。

The JS code is the simple jQuery Modal code that I got off a tutorial at: http://www.queness.com/post/77/simple-jquery-modal-window-tutorial JS代码是我在以下站点获得的一个简单的jQuery Modal代码: http : //www.queness.com/post/77/simple-jquery-modal-window-tutorial

The code in question where I just can't regular expressions to work is here: 有问题的代码在这里我不能使用正则表达式:

$('a[name=modal'.match(/[0-9]/)']').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

And here..... 和这里.....

    //if close button is clicked
    $('.X, #dialog'.match(/[0-9]/)).click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, #dialog'.match(/[0-9]/)).hide();
    }); 

What am I doing wrong? 我究竟做错了什么? Thank you in advance for the help. 预先感谢您的帮助。

Use the jQuery selector $("[name^='modal']"). 使用jQuery选择器$(“ [name ^ ='modal']”)。 See http://api.jquery.com/attribute-starts-with-selector/ 参见http://api.jquery.com/attribute-starts-with-selector/

You can use a data attribute on your modals to pass data to your modal open function or a class , or partially match the name: below is an example of using a class to trigger the event and you simply need to retrive the id of the dialog to open. 您可以在模态上使用data属性将数据传递给模态打开函数或类,或部分匹配名称:以下是使用类触发事件的示例,您只需要获取对话框的ID打开。

Fiddle here 在这里摆弄

HTML 的HTML

    <div class="button_menu">
    <a href="#dialog0" name="modal0" class="open-dialog"/><div class="button">HQ</div></a>
</div>
<div class="button_menu">
    <a href="#dialog0" name="modal1" class="open-dialog"/><div class="button">HQ</div></a>
</div>
<div id="modal0" class="my-dialog">Dialog 1</div>
<div id="modal1" class="my-dialog">Dialog 1</div>

jQuery jQuery的

   $(function(){

    //initialize all your dialogs at once
    $( ".my-dialog" ).dialog({ autoOpen: false });



    //bind to your open-dialog (added to your links) class
    $(".open-dialog").on("click", function(e,ui) {
        e.stopPropagation();
        e.preventDefault();
 var dialog_name = $(this).attr('name');
 $("#" + dialog_name).dialog("open");
});
})

Use classes and data attributes in your HTML to anchor tag. 在HTML中使用类和数据属性来锚定标签。

<div class="button_menu">
    <a href="#dialog0" name="modal0" data-link=0 class="modalsLink" /><div class="button">HQ</div></a>
</div>

Then using jQuery: 然后使用jQuery:

$(".modalsLink").click(function () { 
        //Cancel the link behavior
        e.preventDefault();
        var modalNo = $(this).data("link");
        //Get the A tag
        var id = $(this).attr('href');

        //And do more stuff here.

});

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

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