简体   繁体   English

结合使用jquery-store-locator-plugin(v2)和多种形式

[英]Using jquery-store-locator-plugin (v2) with Multiple Forms

I'm trying to get version 2 of the jquery-store-locator-plugin to work on a dynamically generated page of location results with little success. 我正在尝试使jquery-store-locator-plugin的版本2在动态生成的位置结果页面上工作,但收效甚微。

The problem is that I have a form for each location and I'm unsure how to hook up the plugin to the form when its submit button is clicked. 问题是我在每个位置都有一个表单,并且不确定如何在单击其提交按钮时将插件连接到表单。 Can someone please help me with that? 有人可以帮我吗?

For now, I'm just running the demo found on: 现在,我只是运行以下示例:

https://bjornblog.com/storelocator/v2/ https://bjornblog.com/storelocator/v2/

In the container, there is a form: 在容器中,有一种形式:

    <div class="bh-sl-container">
  <div id="page-header">
    <h1 class="bh-sl-title">Using Chipotle as an Example</h1>
    <p>I used locations around Minneapolis and the southwest suburbs. So, for example, Edina, Plymouth, Eden Prarie, etc. would be good for testing the functionality. 
    You can use just the city as the address - ex: Edina, MN.</p>
  </div>

  <div class="bh-sl-form-container">
    <form id="bh-sl-user-location" method="post" action="#">
        <div class="form-input">
          <label for="bh-sl-address">Enter Address or Zip Code:</label>
          <input type="text" id="bh-sl-address" name="bh-sl-address" />
        </div>

        <button id="bh-sl-submit" type="submit">Submit</button>
    </form>
  </div>

  <div id="bh-sl-map-container" class="bh-sl-map-container">
    <div id="bh-sl-map" class="bh-sl-map"></div>
    <div class="bh-sl-loc-list">
      <ul class="list"></ul>
    </div>
  </div>

In the demo, the storeLocator() function is called at the bottom of the page ( whereas I'd like to invoke it on the button click or form submit). 在演示中,在页面底部调用storeLocator()函数(而我想在单击按钮或提交表单时调用它)。

    $(function() {
        $('#bh-sl-map-container').storeLocator();
    });

In my JSP page, I have a loop that inserts a form within a table row: 在我的JSP页面中,我有一个循环,该循环将表格插入表行中:

    <tbody>
       <c:forEach var="product" items="${productList}" varStatus="loop">
            <tr>
               <td class="product_name"><a href="${product.detailsLink}" target="_blank">${product.name}</a></td>
               <td class="thumb"><a href="${product.detailsLink}" target="_blank"><img src="${product.imagePath}" alt="${product.name}" /></a></td>
               <td class="desc">${product.desc}</td>
               <td class="price">${product.price}</td>
               <td class="">
                 ${product.storeName}
                 <div id="frmContainer${loop.index}">
                   <form id="frmMapIt${loop.index}" class="map_it" onsubmit="go($('#frmContainer${loop.index}'), 'frmMapIt${loop.index}', 'bh-sl-address${loop.index}')">
                        <input type="text" id="bh-sl-address${loop.index}" name="bh-sl-address${loop.index}" />

                        <button id="bh-sl-submit${loop.index}" type="submit">Map it!</button>
                   </form>      
                 </div>
               </td>
           </tr>
       </c:forEach>
     </tbody>
   </table>

This is the function that invokes the storeLocator function: 这是调用storeLocator函数的函数:

    function go(formContainer, formID, addressID) {     
  $('#bh-sl-map-container').storeLocator({
    'dataType': 'json',
    'dataLocation': 'data/locations.json',
    'slideMap': false,
    'modal': true,
    'formContainer': formContainer,
    'formID': formID,
    'addressID': addressID
  });

} }

Thanks! 谢谢!

Rob

Sadly, no one responded to my question, so I set out to answer it myself. 可悲的是,没有人回答我的问题,所以我开始自己回答。

Took some doing, but I got it working eventually. 做了一些工作,但我最终使它工作了。 Here's what I did. 这就是我所做的。

This is the HTML. 这是HTML。 There is one map for all the forms: 所有形式都有一张地图:

<div class="bh-sl-container">
   <div id="page-header">
     <h1 class="bh-sl-title">Using Chipotle as an Example</h1>
     <p>I used locations around Minneapolis and the southwest suburbs. So, for example, Edina, Plymouth, Eden Prarie, etc. would be good for testing the functionality. 
     You can use just the city as the address - ex: Edina, MN.</p>
   </div>

   <div id="" class="formContainer">
     <form id="form1" method="post" action="#">
         <div class="form-input">
           <label for="address1">Enter Address or Zip Code:</label>
           <input type="text" id="address1" name="address1" />  <!-- bh-sl-address -->
         </div>

         <button class="formSubmit" id="submit1" type="button">Submit</button>
     </form>
   </div>

<div id="" class="formContainer">
     <form id="form2" method="post" action="#">
         <div class="form-input">
           <label for="address2">Enter Address or Zip Code:</label>
           <input type="text" id="address2" name="address2" />  <!-- bh-sl-address -->
         </div>

         <button class="formSubmit" id="submit2" type="button">Submit</button>
     </form>
   </div>
   <div id="mapContainer" class="bh-sl-map-container">
     <div id="bh-sl-map" class="bh-sl-map"></div>
     <div class="bh-sl-loc-list">
       <ul class="list"></ul>
     </div>
   </div>
 </div>

Here is my JS. 这是我的JS。 The trick is to reset the map container before invoking the map plugin, as the storeLocator() function changes the HTML dramatically: 技巧是在调用地图插件之前重置地图容器,因为storeLocator()函数会极大地更改HTML:

    $(function() {
        var mapContainer = $('#mapContainer').get(0).outerHTML;
        $('button.formSubmit').one( 'click', function(evt) {
             //change button type for future clicks
             $(this).attr('type', 'submit');
             //stop form from submitting
             $(evt.target.form).on('submit', function(evt) { evt.preventDefault(); });
             //reinit the map container
             $('div.bh-sl-overlay').replaceWith(mapContainer);
             //invoke the plugin
             $('#mapContainer').storeLocator({
               'slideMap':      false,
               'modal':         true,
               'formContainer': 'formContainer',
               'formID':        evt.target.form.id,
               'addressID':     $(evt.target.form).find("input[id^='address']").attr('id')
             })
             .submit();
        });
    });

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

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