获取具有特定ID

时间:2016-10-26 10:31:41

标签: javascript jquery html

我有由symphony创建的动态页面,客户端我试图获取具有通配符(id ^ =' route_segments_)的特定id的触发元素之前的最接近元素。不幸的是,JQuery没有找到任何东西。

该脚本应返回以下项目。

  <div class="col-sm-10">
<input type="text" id="route_segments_1_destCountry" name="route[segments][1][destCountry]" required="required" class="form-control" value="GB">

&#13;
&#13;
$("input:radio").on('change', function() {
  var date = moment($(this).parent().text(), "HH:mm DD MMM YYYY");

  alert($(this).parent().prevAll("input[id^='route_segments_']").first());

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label class="col-sm-2 control-label required" for="route_segments_1_details">Details</label>
  <div class="col-sm-10">
    <textarea id="route_segments_1_details" name="route[segments][1][details]" required="required" class="form-control"></textarea>
  </div>
</div>

<div class="form-group">
  <label class="col-sm-2 control-label required" for="route_segments_1_vehicle">Vehicle</label>
  <div class="col-sm-10">
    <input type="text" id="route_segments_1_vehicle" name="route[segments][1][vehicle]" required="required" class="form-control" value="Bus">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label required" for="route_segments_1_vehicleKind">Vehicle kind</label>
  <div class="col-sm-10">
    <input type="text" id="route_segments_1_vehicleKind" name="route[segments][1][vehicleKind]" required="required" class="form-control">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label required" for="route_segments_1_originCountry">Origin country</label>
  <div class="col-sm-10">
    <input type="text" id="route_segments_1_originCountry" name="route[segments][1][originCountry]" required="required" class="form-control" value="GB">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label required" for="route_segments_1_destCountry">Dest country</label>
  <div class="col-sm-10">
    <input type="text" id="route_segments_1_destCountry" name="route[segments][1][destCountry]" required="required" class="form-control" value="GB">
  </div>
</div>

<button type="button" style="cursor: pointer" class="showHidden btn btn-default">Change Places</button>
</div>
<div class="col-sm-6" style="height: 100%">
  <strong>Transit Duration: </strong>0h 21m
  <br>
  <strong>Transfer Duration: </strong>0h 4m
</div>
</div>
</div>
<div style="overflow:auto;height: 250px;border: 1px solid #686868;border-radius: 5px;width:80%;margin: auto;margin-top:20px;margin-bottom: 20px">



  <div style="margin: auto;width: 90%">
to;width: 90%">
                <div class="form-group"><div class="col-sm-2"></div><div class="col-sm-10"><div class="radio"><label class="required"><input type="radio" id="route_flight_0" name="route[flight]" required="required" value="0"> Dubai 00:30 26 Oct 2016 Dublin 14:55 26 Oct 2016
                Duration: 17h 25m  Stops: 3  Price: 393.5 EUR</label></div></div></div>
                </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <div style="margin: auto;width: 90%">
                <div class="form-group"><div class="col-sm-2"></div><div class="col-sm-10"><div class="radio"><label class="required"><input type="radio" id="route_flight_1" name="route[flight]" required="required" value="1"> Dubai 00:30 26 Oct 2016 Dublin 09:10 26 Oct 2016
                Duration: 11h 40m  Stops: 2  Price: 402.27 EUR</label></div></div></div>
                </div>
                                 
    <div class="form-group">
      <div class="col-sm-2"></div>
      <div class="col-sm-10">
        <div class="radio">
          <label class="required">
            <input type="radio" id="route_flight_0" name="route[flight]" required="required" value="0">Dubai 00:30 26 Oct 2016 Dublin 14:55 26 Oct 2016 Duration: 17h 25m Stops: 3 Price: 393.5 EUR</label>
        </div>
      </div>
    </div>
  </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您将遇到此HTML结构的问题,因为<input type="radio">与您尝试定位的元素之间的DOM之间没有明确的关系。

具体来说,jQuery的.prevAll仅查看您开始的元素的前一个兄弟姐妹:在这种情况下,<label class="required">是其父元素你的单选按钮。该标签没有兄弟姐妹,所以prevAll什么都不返回。

有几种方法可以定义无线电与可能目标之间的关系。一种方法是将单选按钮和目标包装在一个已知容器中,然后在容器内查找最后一个匹配:

<强> HTML:

<ul class="routes">
  <li class="route">
     <input type="text" class="route-segment" value="route1"/>
  </li>
  <li class="route">
     <input type="text" class="route-segment" value="route2"/>
  </li>
  <li class="route">
     <input type="text" class="route-segment" value="route3"/>
  </li>
    <li class="route">
     <input type="text" class="route-segment" value="route4"/>
  </li>
</ul>

<ul class="transfers">
  <li class="transfer">
    <input type="radio" name="transfer-select" value="transfer-1">
  </li>
  <li class="transfer">
    <input type="radio" name="transfer-select" value="transfer-2">
  </li>
</ul>

<强> JS:

$(function(){
    // Cache the elements that we're going to need:
  var routes = $('.routes .route'),
      transfers = $('.transfers .transfer');

    // Whenever the selected transfer is changed, find the last route:
  transfers.on('change', 'input:radio', function(){
    var route = routes.last();
    //Find the input inside the last route:
    console.log(route.find('input.route-segment').val());
    //Find the input inside the second-to-last route:
    console.log(route.prev().find('input.route-segment').val());
  });
});