如何使用jQuery获取父元素值? [英] How to get the parent element value using jquery?

查看:98
本文介绍了如何使用jQuery获取父元素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,该表有几行,每行有两个单元格,每个单元格都有几个信息,例如办公室名称,地址和电话号码等.使用jquery分别从每个单元格中提取地址,然后馈入google map geocoder对象以获取点并将其绘制在地图中.现在,在每次命中一个Address值时,我还想从jquery正在从中获取地址值的当前单元格中获取电话和办公室名称的唯一值.我需要这些值,以便可以在InfoWindow的InfoWindow中显示这些值.地图?我如何获得这些价值?

I have a table with a couple of rows with two cells in each row and each cell has couple of info like office name, address and phone number etc. Using jquery for each, the address is being pulled out from each cell and fed into google map geocoder object to get the point and plot it in the map. Now at each hit of an Address value, I would also like to grab the unique value of phone and office name from the current cell from which jquery is getting address value..I need those values so i can display those values in the InfoWindow of the map? How do I get those values?

<table class="OfficeInfo" border="0" style="width: 100%" cellspacing="10px" cellpadding="15px">
  <tr>
    <td class="Office1" style="width=40%">  
     <span class="OfficeName">
     <div id="ctl00_PlaceHolderMain_ctl00_ctl17_label" style='display:none'>Office1Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl17__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl17_label"><div class="ms-rtestate-field"><a href="/" target="_blank">St. Francis Hospital</a></div></div>
     </span>
     <span class="Address">
     2001 86th Street West  <br />Indianapolis, IN 46260        
     </span> <br />
     <span class="Phone">
     (402) 123-1234</span><br /><br />
     <a class="mapdirectionsLink" href="#">map &#38; directions&#62;</a><br /><br />
     <span class="Hours">
     MTW:9:00AM-5:00PM</span>
    </td>

    <td class="Office2" style="width:40%">  
     <span class="OfficeName">
     <div id="ctl00_PlaceHolderMain_ctl00_ctl21_label" style='display:none'>Office2Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl21__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl21_label"><div class="ms-rtestate-field"><a href="/" target="_blank">St. Margaret's Hospital</a></div></div>
     </span>    
     <span class="Address">
     8075 North Shadeland Avenue <br />Indianapolis, IN 46250</span><br />
     <span class="Phone">
     (316) 123-3245</span><br /><br />
     <a class="mapdirectionsLink" href="#">map &#38; directions&#62;</a><br /><br />
     <span class="Hours">
     MTW:9:00AM-5:00PM</span>
    </td>
  </tr> 

   <tr>                                   
    <td class="Office3" style="border-top:1px dotted silver;  width:40%;">
     <span class="OfficeName">
     <div id="ctl00_PlaceHolderMain_ctl00_ctl25_label" style='display:none'>Office3Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl25__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl25_label"><div class="ms-rtestate-field"><a href="/" target="_blank">Munster Women's Center</a></div></div>
     </span>
     <span class="Address">
     395 Westfield Road <br />Noblesville, IN 46060</span><br />
     <span class="Phone">
     (316) 123-3245</span><br /><br />  
     <a class="mapdirectionsLink" href="#">map &#38; directions&#62;</a><br /><br />
     <span class="Hours">
     MTW:9:00AM-5:00PM</span>
    </td>
    <td  style="border-top:1px dotted silver;  width:40%">                          
    </td>
  </tr>               

 </table>


          function codeAddress() {
    var infowindow = new google.maps.InfoWindow({}); 
    $('span.Address').each(function(index) {
        var addy = $(this).text();
        var off_name = $(this).siblings('.OfficeName').children(.ms-rtestate-field).text();
        var infowindow = new google.maps.InfoWindow({  content: 'Hello world'   });

        geocoder.geocode( { 'address': addy}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                title:addy          
            });


            //var currentCell=(this).closest(td).html()); 
            // Adding a click event to the marker 
            google.maps.event.addListener(marker, 'click', function() { 
                infowindow.setContent('<span style="color:#808080; font-size:13px; font-family:Trebuchet">'+addy 
                           +'<br> <a href="http://maps.google.com/maps?saddr=&daddr=' + this.position.toUrlValue() + '" target ="_blank">Get Direction To Here<\/a>'+                      
                           off_name + '</span>'); 
                infowindow.open(map, this); 
            });  


            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    });
}

推荐答案

根据this的当前上下文,这应该可以工作:

Depending on the current context of this, then this should work:

$(this).parent().find('.officeName').html();

这篇关于如何使用jQuery获取父元素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆