dongluobei9359 2014-10-16 00:32
浏览 35
已采纳

具有多种形式的bootstrap单模态窗口

I have a form submission on a modal, and it works fine for one form but there needs to be 15 modal forms on one page. How can I bind the specific modal opener fields to each form?

I'm sure I'm not explaining myself very well, so here's an example. This is part of a pricing table, so each div set would submit their own period selection and hidden fields - initiated by each individual modal window opener.

<form action="<?php echo JRoute::_('index.php?option=com_component&view=cart'); ?>" enctype="multipart/form-data" method="post">
    <div class="col col-md-4 no-padding">
        <div class="col-header text-center">
            <h3>Single Domain</h3>
            <h3>$4.99/Mo</h3>
        </div>
        <div class="col-body">
            <ul>
                <li class="row0 yes">Single Domain</li>
                <li class="row1 yes">Unlimited Web Space</li>
                <li class="row0 yes">Unlimited Bandwidth</li>
                <li class="row1 yes">Unlimited Mail Accounts</li>
                <li class="row0">
                    <select name="periods">
                        </option value="12">12 Months</option>
                        </option value="24">24 Months</option>
                        </option value="36">36 Months</option>
                    </select>
                </li>
            </ul>
            <input type="hidden" name="plan_name" value="Single" />
            <input type="hidden" name="plan_type" value="Windows_Hosting" />
            <input type="hidden" name="plan_id" value="123456" />
        </div>
        <div class="col-footer text-center">
            <!-- Button trigger modal -->
            <a href="#myModal" class="btn btn-lg btn-default btn-primary" data-toggle="modal" data-target="#myModal">Buy Now</a>
            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog-md">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                            <h4 class="modal-title" id="myModalLabel">Specify a domain name for your order</h4>
                        </div>
                        <div class="modal-body">
                            <label class="radio-inline">
                                <input type="radio" name="has_domain" id="inlineRadio1" value="option1"> I already have a Domain Name
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="register_domain" id="inlineRadio1" value="option1"> I want to buy a new Domain Name 
                            </label>
                            <input type="text" placeholder="www." id="inputDomain" class="form-control">
                            <div class="row">
                                <strong>Building a website for your business? Don't risk it - safeguard your website with our must-have website tools</strong>
                                <div class="col-sm-6">
                                    <div class="media">
                                        <span class="pull-left sitelock-menu-icon"> </span>
                                        <div class="media-body">
                                            <h4 class="media-heading text-left">Sitelock</h4>
                                            <p class="text-left">Over 5000 websites get attacked everyday. Get SiteLock and secure your website from hackers, viruses and malware.</p>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="media">
                                        <span class="pull-left codeguard-menu-icon"> </span>
                                        <div class="media-body">
                                            <h4 class="media-heading text-left">CodeGuard</h4>
                                            <p class="text-left">Protect yourself from unexpected website crashes. Add CodeGuard and get automatic cloud backup for your website and database.</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default btn-small" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary btn-small">Continue to checkout</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- End Modal -->
        </div>
    </div>

EDIT:

I think I included too much code, and not even explanation, so here is a shorter/better example.

<form action="index.php?option=com_component&view=cart" enctype="multipart/form-data" method="post">
    <!-- Set #1 -->
    <select name="periods_single">
        </option value="12">12 Months</option>
        </option value="24">24 Months</option>
        </option value="36">36 Months</option>
    </select>
    <input type="hidden" name="plan_name" value="Single" />
    <input type="hidden" name="plan_type" value="Windows_Hosting" />
    <input type="hidden" name="plan_id" value="123456" />
    <!-- Button trigger modal -->
    <a href="#myModal" class="btn btn-lg btn-default btn-primary" data-toggle="modal" data-target="#myModal">Buy Now</a>

    <!-- Set #2 -->
    <select name="periods_double">
        </option value="12">12 Months</option>
        </option value="24">24 Months</option>
        </option value="36">36 Months</option>
    </select>
    <input type="hidden" name="plan_name" value="Double" />
    <input type="hidden" name="plan_type" value="Windows_Hosting" />
    <input type="hidden" name="plan_id" value="234567" />
    <!-- Button trigger modal -->
    <a href="#myModal" class="btn btn-lg btn-default btn-primary" data-toggle="modal" data-target="#myModal">Buy Now</a>

    <!-- Set #3 -->
    <select name="periods_triple">
        </option value="12">12 Months</option>
        </option value="24">24 Months</option>
        </option value="36">36 Months</option>
    </select>
    <input type="hidden" name="plan_name" value="Triple" />
    <input type="hidden" name="plan_type" value="Windows_Hosting" />
    <input type="hidden" name="plan_id" value="345678" />
    <!-- Button trigger modal -->
    <a href="#myModal" class="btn btn-lg btn-default btn-primary" data-toggle="modal" data-target="#myModal">Buy Now</a>            
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog-md">
            <div class="modal-content">
                    <input type="text" placeholder="www." id="inputDomain" class="form-control">
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-small" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary btn-small">Continue to checkout</button>
                    </div>
            </div>
        </div>
    </div>
    <!-- End Modal -->
</form>

Ok, so now you can see that there are (in this example) three sets of period options, each with hidden fields and their modal opener that points to a single modal. What I'm trying to accomplish is to know which modal opener was used, and then get the values associated with it.

  • 写回答

1条回答 默认 最新

  • dream02008 2014-10-16 20:15
    关注

    Alright, here we go. I think HTML5 data attributes will do it for you. Keep in mind the code I shared below extremely minimal. Have a look at my JSFiddle for a more complete example.

    By doing this, you only need one set of hidden fields (the fields that you are going to submit with the modal form). This is what happens:

    • User clicks on a product. The button clicked has data attributes associated with it which get passed along to the modal form.
    • A modal opens with an form inside (you can make this hidden, they are visible to demonstrate) the form now holds the correct data.
    • User proceeds, the form will be submitted.

      $(document).ready(function() {
      
        // You'll probably wan't to make .btn-default more specific
        $('.btn-default').on( 'click', function() {
      
          var name = $(this).attr('data-name');
          var type = $(this).attr('data-type');
          var id = $(this).attr('data-id');
          var option = $(this).parents('.product').find(':selected').attr('data-option');
      
          $('.modal input#choosen_option').val(option);
          $('.modal input#choosen_name').val(name);
          $('.modal input#choosen_type').val(type);
          $('.modal input#choosen_id').val(id);
      
       });
      
      });
      

    The markup:

     <!-- Set #1 --> 
     <div class="product">
       <select name="periods_single">
        <option data-option="12">12 Months</option>
        <option data-option="24">24 Months</option>
        <option data-option="36">36 Months</option>
       </select>
     <a class="btn-default" data-name="Single" data-type="Windows_Hosting" data-id="1">  
       Buy Now
     </a>
     </div>
    
     <!-- Set #2 --> 
      <div class="product">
       <select name="periods_double">
        <option data-option="12">12 Months</option>
        <option data-option="24">24 Months</option>
        <option data-option="36">36 Months</option>
       </select>
     <a class="btn-default" data-name="Double" data-type="Windows_Hosting" data-id="2">Buy Now</a>
      </div>
    

    The modal with form:

    <!-- Modal -->
     <div class="modal fade" id="myModal">
        <div class="modal-dialog-md">
            <div class="modal-content">
                <div class="modal-footer">
                    <form action="">
                    <input type="text" id="choosen_option" />
                    <input type="text" id="choosen_name" />
                    <input type="text" id="choosen_type" />
                    <input type="text" id="choosen_id" />
                    </form>
                </div>
            </div>
        </div>
     </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集