如何使用jquery获取父元素值?

时间:2011-11-17 21:58:07

标签: jquery element parent siblings parents

我有一个包含几行的表,每行有两个单元格,每个单元格都有一些信息,如办公室名称,地址和电话号码等。每个都使用jquery,从每个单元格中取出地址输入谷歌地图地理编码器对象获取点并将其绘制在地图中。现在每次点击一个地址值,我还想从当前单元格获取电话和办公室名称的唯一值,jquery从中获取地址值。我需要这些值,所以我可以在InfoWindow中显示这些值地图?我如何获得这些价值观?

<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);
            }
        });
    });
}

6 个答案:

答案 0 :(得分:5)

根据this的当前上下文,这应该有效:

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

答案 1 :(得分:2)

$(this).siblings('.Phone').html()$(this).siblings('.Address').html()

答案 2 :(得分:2)

我们可以使用jquery或Javascript来做到这一点。在这里,我们将讨论电子邮件ID更新。

在下面的示例中,将打开一个弹出窗口,其中包含来自父窗口的自动填充电子邮件ID。 更新后,电子邮件将在父窗口文本框中自动更新,弹出窗口将自动关闭。

示例:

1)将文件index.html创建为父窗口

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<title></title>
<head></head>

<body>
<table>

<tr>
<td colspan=”2″>Example for update email id.</td>
</tr>
<tr>
<td>Email Id:</td>
<td>
<input type=’text’ name=”emailID” id=”emailId” value=”demo@demo.com”></td>
</tr>
<tr>
<td>
<input type=”button” name=”update” value=”Update”
onClick=’window.open(“update_popup.html”, “”, “width=400, height=300″)’>
</td>
</tr>
</table>
</body>
</html> 

2)创建文件update_popup.html作为弹出窗口,其中电子邮件ID从父窗口自动填充以进行更新。

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<title></title>
<head></head>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
<script>
$(document).ready(function(){

//== pre fill parent window email id in popup window for update

var emailId = window.opener.document.getElementById(“emailId”).value;
$(“#emailId”).val(emailId);

//=== update updated email id in to parent window

$(“#Save”).on(‘click’,function(){

var updated_emailId = $(“#emailId”).val();
window.opener.document.getElementById(“emailId”).value = updated_emailId;
window.close();
});
});
</script>

<body>
<table>
<tr>
<td>Email Id:</td>
<td>
<input type=’text’ name=”emailID” id=”emailId” value=””></td>
</tr>
<tr>
<td><input type=”button” name=”Save” id=”Save” value=”Save”></td>
</tr>
</table>
</body>
</html>

了解更多详情..

http://www.delhincrjob.com/blog/how-to-get-the-parent-window-element-value-in-popup-window-using-jquery/

答案 3 :(得分:0)

$(this).parent().find(".OfficeName").text()获取办公室名称,$(this).parent().find(".Phone").text()获取每个地址的电话。

jsfiddle - 点击一个地址,它会在警报中显示姓名和电话。

答案 4 :(得分:0)

$('td.Office1').chilldren('.Phone')

将从Office 1 td

获取Office 1的电话号码
$('span.Address').siblings('.Phone')

将通过地址范围

获取电话号码

答案 5 :(得分:0)

这是一个小而简单但适合初学者,Jquery的PDF。尝试使用Jquery Cheat Sheet,因为它包含解决您大部分问题的方法。