2017-07-10 100 views
0

按照下表,我如何在選擇更改時填充行輸入?在選擇更改後填充輸入

我有幾行作爲一個。

<table class="table table-striped table-bordered"> 
    <tbody> 
     <tr> 
      <th width="60%">Date</th> 
      <th width="40%">Rate</th> 
     </tr> 
     <tr> 
      <th>Today<br><select name="RAT_Rates" class="form-control"><option data-name="" data-description="" data-rate="" selected="">Custom</option><option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option></select></th> 
      <td> 
       <div class="col-sm-12"> 
        <input type="text" name="BIL_Rate[]" class="form-control" required=""> 
       </div> 
      </td> 
     </tr> 
     <tr> 
      <th>Tomorrow<br><select name="RAT_Rates" class="form-control"><option data-name="" data-description="" data-rate="" selected="">Custom</option><option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option></select></th> 
      <td> 
       <div class="col-sm-12"> 
        <input type="text" name="BIL_Rate[]" class="form-control" required=""> 
       </div> 
      </td> 
     </tr> 
    </tbody> 
</table> 

我親眼:

$('select[name=RAT_Rates]').on('change', function() { 
    var selected = $(this).find('option:selected'); 

    var name = selected.attr('data-name'); 
    var description = selected.attr('data-description'); 
    var rate = selected.attr('data-rate'); 

    $(this).next('input[name=BIL_Rate]').val(rate); 
}); 

非常感謝。

+0

的''的標籤input'的name'屬性是「BIL_Rate []」。括號是什麼? – Chet

+0

導致最終結果應該像數組一樣對待。我有這樣的幾排。 – user8201518

回答

0

要使用屬性選擇器,你需要使用兩套報價。
一套選擇器本身...
而你需要一個屬性值。

所以$('select[name=RAT_Rates]')應該是$("select[name='RAT_Rates']")

然後(這是次要的,但有效的HTML總是有幫助),您的select元素未關閉。

$("select[name='RAT_Rates']").on('change', function() { 
 

 
    var selected = $(this).find('option:selected'); 
 

 
    var name = selected.attr('data-name'); 
 
    var description = selected.attr('data-description'); 
 
    var rate = selected.attr('data-rate'); 
 

 
    $(this).closest("tr").find("input[name='BIL_Rate[]']").val(rate); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table class="table table-striped table-bordered"> 
 
    <tbody> 
 
    <tr> 
 
     <th width="60%">Date</th> 
 
     <th width="40%">Rate</th> 
 
    </tr> 
 
    <tr> 
 
     <th> 
 
     Today<br> 
 
     <select name="RAT_Rates" class="form-control"> 
 
      <option data-name="" data-description="" data-rate="" selected="">Custom</option> 
 
      <option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option> 
 
     </select> 
 
     </th> 
 
     <td> 
 
     <div class="col-sm-12"> 
 
      <input type="text" name="BIL_Rate[]" class="form-control" required=""> 
 
     </div> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+0

你好,你的解決方案工程布,除非我只有一行,這是不幸的不是我的情況。我有幾行這樣的,所以你的代碼一旦選擇變化,填寫所有其他輸入的值。我只需要在同一行上的一個。對不起,如果不清楚。 – user8201518

+0

我編輯...沒有問題! ;) –

0

您可以使用parent()並使用find()在裏面找到類。下面請我的更新代碼:

HTML

<table class="table table-striped table-bordered"> 
<tbody> 
    <tr> 
     <th width="60%">Date</th> 
     <th width="40%">Rate</th> 
    </tr> 
    <tr> 
     <th>Today<br><select name="RAT_Rates" class="form-control"><option data-name="" data-description="" data-rate="" selected="">Custom</option><option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option></select></th> 
     <td> 
      <div class="col-sm-12"> 
       <input type="text" name="BIL_Rate[]" class="form-control input_rate" required=""> 
      </div> 
     </td> 
    </tr> 
    <tr> 
     <th>Today<br><select name="RAT_Rates" class="form-control"><option data-name="" data-description="" data-rate="" selected="">Custom</option><option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option></select></th> 
     <td> 
      <div class="col-sm-12"> 
       <input type="text" name="BIL_Rate[]" class="form-control input_rate" required=""> 
      </div> 
     </td> 
    </tr> 
    <tr> 
     <th>Today<br><select name="RAT_Rates" class="form-control"><option data-name="" data-description="" data-rate="" selected="">Custom</option><option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option></select></th> 
     <td> 
      <div class="col-sm-12"> 
       <input type="text" name="BIL_Rate[]" class="form-control input_rate" required=""> 
      </div> 
     </td> 
    </tr> 
</tbody> 

的Javascript:

$('select[name=RAT_Rates]').on('change', function() { 
var selected = $(this).find('option:selected'); 

var name = selected.attr('data-name'); 
var description = selected.attr('data-description'); 
var rate = selected.attr('data-rate'); 

$(this).parent().parent().find('.input_rate').val(rate); 
}); 
+0

你好,因爲我有幾行像我的問題,我現在無法使用你的解決方案。抱歉。 – user8201518

+0

Ohh ..好吧..我更新了我的答案..檢查出來.. – smzapp

0

這裏是你想要的東西 -

$('select[name=RAT_Rates]').on('change', function() { 
 
    var selected = $(this).find('option:selected'); 
 

 
    var name = selected.attr('data-name'); 
 
    var description = selected.attr('data-description'); 
 
    var rate = selected.attr('data-rate'); 
 
    $(this).closest('td').next('td').find($("input[name='BIL_Rate[]']")).val(rate); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table table-striped table-bordered"> 
 
    <tbody> 
 
     <tr> 
 
      <th width="60%">Date</th> 
 
      <th width="40%">Rate</th> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
      <div> 
 
      Today 
 
      </div> 
 
       <select name="RAT_Rates" class="form-control"> 
 
       <option data-name="" data-description="" data-rate="" selected="">Custom</option> 
 
       <option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option> 
 
       </select> 
 
      </td> 
 
      <td> 
 
       <div class="col-sm-12"> 
 
        <input type="text" name="BIL_Rate[]" class="form-control" required=""> 
 
       </div> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
     <td><div> 
 
     Tomorrow 
 
     </div> 
 
      <select name="RAT_Rates" class="form-control"><option data-name="" data-description="" data-rate="" selected="">Custom</option><option data-name="Special" data-description="Special" data-rate="99.99"> Special - $ 99.99</option></select> 
 
      </td> 
 
      <td> 
 
       <div class="col-sm-12"> 
 
        <input type="text" name="BIL_Rate[]" class="form-control" required=""> 
 
       </div> 
 
      </td> 
 
     </tr> 
 
    </tbody> 
 
</table>

感謝

+0

我有幾行像我的問題中提到的一個。我認爲你的代碼會填滿我所有的輸入嗎? – user8201518

+0

如果你有多行,那麼多個下拉列表? – Harsheet

+0

是的,我更新了這個問題,因爲它一開始就不清楚。抱歉。 – user8201518