2013-10-21 23 views
0

我有一個表格行看起來像enter image description herejQuery的:錶行

的HTML看起來像 enter image description here

當用戶更改名稱字段中的我通過「選擇」裏面的值獲得選擇命名爲DropChanged函數。獲取參考號後,選擇我想更改優惠券數量選擇框的值。我如何使用Jquery獲得對它的引用?表中還會有很多這樣精確的行。

更新:這裏是虛擬的HTML代碼和jsfiddle link

<table id="competitors_table" width="100%" style="border-bottom: #000000 solid 1px;"> 
<tbody> 
    <tr> 
     <td>Name:<select name="CompetitorIDs_Setting_[]" onchange="nameDropDownChanged(this);"> 
      <option value="0">N/A</option><optgroup label="Similar Stores"></optgroup> 
      <optgroup label="Other Stores"> 
      <option value="1">flipkart</option> 
      <option value="2">bigshoebazaar</option> 
      <option value="160">presto</option> 
      <option value="3">fabindia</option> 
      <option value="4">fashnvia</option> 
     </td> 
     <td> 
      <select name="Position_Setting_[]" onchange="dropDownChanged(this);"> 
       <option value="1000">Top</option> 
       <option value="1001">Middle</option> 
       <option value="1002">Bottom</option>              
       <option value="">Enter Position</option> 
      </select> 
      <input type="text" name="PositionNumber_Setting_[]" size="3" style="display: none;"> 
     </td> 
     <td>Number of Coupons:<select id="numberOfCoupons_Setting_[]"></select></td> 
    </tr> 
</tbody> 
</table> 

Javascript代碼

function nameDropDownChanged(select) 
{ 
    var selectedIndex = select.selectedIndex; 
    var WebsiteName = select.options[selectedIndex].innerHTML; 
    var couponCount = <?php if(!empty($couponCount)){ echo json_encode($couponCount); }?>; 

    if(Object.keys(couponCount).length > 0) 
    { 
     var numberOfCoupons = couponCount[WebsiteName]; 
     var numberOfCouponsDropDown = document.getElementById('numberOfCouponsSelect'); 
     $("#numberOfCouponsSelect").empty(); 
     if(numberOfCoupons > 0) 
     { 
      for(var i=1; i <= numberOfCoupons; i++) 
      { 
       var option = document.createElement('option'); 
       option.value = i; 
       option.innerHTML = i; 
       numberOfCouponsDropDown.appendChild(option); 
      } 
     } 
     else 
     { 
      var option = document.createElement('option'); 
      option.value=1; 
      option.innerHTML = "No Active Coupons"; 
      numberOfCouponsDropDown.appendChild(option); 
     } 
    } 
} 
+0

$('#Selector_ID')。change(function(selector){change values in here});請聯繫 –

+0

相關的HTML代碼 – bipen

回答

1

的nameDropChanged函數內部..

// considering 'parameter' is the variable to be passed as a parameter to nameDropChanged function 
$numberSelect = $(parameter).parent().nextAll(':last').children('select'); 
// $numberSelect is now containing a reference to the numberOfCoupons_Setting_[ select] 
1

你可以做這樣的事情

$('#Selector_ID1, #Selector_ID2, #Selector_ID3, ...').change(function() { 
    var parent = $(this).parents("tr"); 
    var elm = $("td:last-child", parent).children("select"); 
    //Do your operation with last Select Element here. 
}); 

這有助於在兩個方面

  1. 你不需要知道確切的父母和孩子,只是扭轉 軌道父母是TR在第一種情況下,然後最後選擇在 的家長。
  2. 在一次去你可以處理多行。你可以在這裏使用類選擇器 。