2017-07-10 101 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