2014-01-15 2 views
0

는 사용자가 자신의 우편 번호를 입력 내 원래의 코드는 하나의 div 작동jQuery를 구글지도 방향

<div class="map-block"> 
      <img src="<?php the_sub_field('images'); ?>" alt="<?php the_sub_field('shop_title'); ?>" /> 

      <div class="map-bottom"> 
       <h3><?php the_sub_field('shop_title'); ?></h3> 
       <a class="findToggle" href="javascript:void(0)">How to find us</a> 
       <p>Our Address: <?php the_sub_field('address');?></p> 
       <form class="map-form"> 
        <input type="text" class="user-postcode" placeholder="Please enter your postcode"> 
        <a class="btn get_directions" href="javascript:void(0)">Find Us</a>    
       </form>    
      </div> 

     </div> 




    $('.map-block').on('click', '.get_directions', function() { 
      var val = $('.user-postcode').val(); 
      if (val.trim() == '' || val === null) {val = "Enter your location"; } 
      window.open("http://maps.google.com/maps?saddr=" + val + "&daddr=CF23 9AF"); 
    }); 

하고있는 지정된 우편 번호에 방향을 얻을 수있는, 내 div의 워드 프레스에 의해 생성되는하기 그래서 각각 다른 ID를 설정하고 jquery를 사용하여 각 하나에 대한 가치를 얻을 수 있지만 몇 가지 이유가 작동하지 않는 이유는 여기에 여러 divs 내 코드를 볼 수 없습니다.

<div class="map-block" id="map-<?php echo $i++ ;?>"> 
      <img src="<?php the_sub_field('images'); ?>" alt="<?php the_sub_field('shop_title'); ?>" /> 

      <div class="map-bottom"> 
       <h3><?php the_sub_field('shop_title'); ?></h3> 
       <a class="findToggle" href="javascript:void(0)">How to find us</a> 
       <p>Our Address: <?php the_sub_field('address');?></p> 
       <form class="map-form"> 
        <input type="text" class="user-postcode" placeholder="Please enter your postcode"> 
        <a class="btn get_directions" href="javascript:void(0)">Find Us</a>    
       </form>    
      </div> 

     </div> 




var mapid = $('.map-block').attr('id'); 
    $(' + mapid + ').on('click', '.get_directions', function() { 
      var val = $('.user-postcode').val(); 
      if (val.trim() == '' || val === null) {val = "Enter your location"; } 
      window.open("http://maps.google.com/maps?saddr=" + val + "&daddr=CF23 9AF"); 
    }); 
+0

정의하십시오 예외가 발생하고 있습니까? 당신이 얻고있는 결과에 비해 어떤 결과가 기대됩니까? –

답변

0
당신은 단지에 우편 번호를 찾을 필요가

div$(this)와 ". 작동하지 않는"

$('.map-block').on('click', '.get_directions', function() { 
    var val = $(this).parent().find('.user-postcode').val(); 
    if (val.trim() == '' || val === null) { 
     val = "Enter your location"; 
    } 
    window.open("http://maps.google.com/maps?saddr=" + val + "&daddr=CF23 9AF"); 
}); 
+0

제공된 컨텍스트에서'this'는'.user-postcode'를 감싸지 않는'.get_directions'를 가리키며'$ (this) .find ('. user-postcode')'는 결코 요소로 해석되지 않습니다. –

+0

답변을 업데이트했습니다. 그 점을 지적 해 주셔서 감사합니다. – putvande

+0

완벽하게 작동합니다. – user41522