简体   繁体   English

使用jQuery异步iframe

[英]asynchronous iframe using jquery

So I'm creating a code portfolio based upon what's in my svn repo. 所以我要根据我的svn存储库中的内容创建一个代码组合。

I'm currently set up like this: 我目前的设置是这样的:

在此处输入图片说明

PHP loops through my JSON and creates these little menus for each file in each project. PHP遍历我的JSON,并为每个项目中的每个文件创建这些小菜单。 Here's some (very poorly structured) code showing how I generate each of these: 这是一些(结构很差的)代码,显示了如何生成这些代码:

<form action = "" method = 'post'><select name = "s2" id = "s2"> <?php
    foreach($proj2->item_vers as $ver)
    {
         ?>
         <option value = <?php
         $base_str = (string)$ver->revision;
         $full_str = "\"".$base_str."\"";
         echo($full_str); ?> > <?php
         echo($base_str); ?> </option>
         <?php

    }   ?> </select><input type = 'submit'/></form>
        <a href = <?php
        if (isset($_POST['2']))
        {
             $rev2 = $_POST['s2'];
        }
        else 
             $rev2 = "";
        $full_url = "https://myrepo".
        $proj2->name."?p=".$rev2;
        $ret_url = "\"".$full_url."\"";
        echo($ret_url); ?> > Code </a></td>

So I have a form , select , option . 所以我有一个formselectoption The select will post the selected version from the drop down. 选择项将从下拉列表中发布所选版本。 Then I have a hyperlink which forms the correct url based upon the posted version number. 然后,我有一个超链接,可根据发布的版本号形成正确的URL。 This hyperlink will need to be replaced with an iframe . 此超链接将需要替换为iframe For the sake of simplicity, lets say that each project has its own iframe which is updated with appropriate project file any time you click one of the submit buttons. 为了简单起见,可以说每个项目都有自己的iframe,只要您单击其中一个提交按钮,就会使用相应的项目文件进行更新。

So like this: 像这样:

在此处输入图片说明

I'm looking for the simplest way to set this up to work asynchronously. 我正在寻找最简单的方法来将其设置为异步工作。

My thoughts: I've already set up an asynchronous comment system using jQuery. 我的想法:我已经使用jQuery建立了异步注释系统。 The difference was that I only had three little inputs and a button to submit the comment. 不同之处在于我只有三个小inputs和一个提交评论的button This just seems like it will be much more complicated as the code I posted above will loop through about 100 times. 这似乎看起来要复杂得多,因为我上面发布的代码将循环大约100次。 I cant just hard code a different id to every single submit button and write 100 different .js scripts to operate when each individual button is clicked. 我不能为每个提交按钮硬编码一个不同的ID,并且在单击每个按钮时编写100个不同的.js脚本进行操作。

OK, so im going to simplify your form generation code to make this clearer. 好的,因此我将简化您的表单生成代码以使其更清楚。 I am including the outer loop you dont show: 我包括您不显示的外部循环:

<div id="forms-holder">
    <!--this is the outer loop-->
    <?php foreach ($projects as $project):?>

    <!-- remove any ids in the loop, they can not be duplicated-->  
    <form action="" class="projectform">

    <!-- add name to data attribute for easy retrieval by js-->
    <select name="s2" data-projectname="<?php echo $project->name;?>">
        <?php foreach ($project->item_vers as $ver):?>
        <option value="<?php echo'"'. $ver->revision .'"';?>"><?php echo $ver->revision;?></option>
        <?php endforeach;?>
    </select>

    <input type = 'submit'/>

    <form>

    <?php endforeach;?>
</div>
<iframe src="" frameborder="0" id="myiframe"></iframe>

<script type="text/javascript">
    $(function(){
        //when any form is submitted
        $('.projectform').submit(function(ev){
            ev.preventDefault();

            //grab revision and project name
            var revision = $(this).find('select').val();
            var projname = $(this).find('select').attr('data-projectname');

            //generate the url
            var iframeurl = "https://myrepo"+projname+"?p="+revision;

            //set the iframes url
            $('#myiframe').attr('src', iframeurl);
        })
    });
</script>

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

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