2014-12-27 129 views
-3

希望你能帮助我,我认为你真的很容易解决:)。为每一行重复代码jquery

我有以下的jQuery脚本:

$('#addpricebtn').click(function() { 



    $('.stidinput').each(function (i) { 

     $.ajax({ 
      type: 'GET', 
      url: '/Builder/rateretriever', 
      datatype: 'json', 
      data: { id: $('.stidinput').val() }, 
      success: function (result) { 
       $('.categoryinput').html(result[i].category + ' - ' + result[i].subcategory + ' - ' + result[i].site); 
       $('.formatinput').html(result[i].format); 
       $('.exclusiveinput').html(result[i].exclusive); 
       $('.cminput').html(result[i].costmethod); 
      } 
     }); 
    }); 
}); 

这个脚本工作,但只着眼于首先填写ID,我想使用的代码的每一行,这就是为什么我有下面的代码...(我想用不同的ID使用相同的请求对每一个是越来越被用户:)填充线)

$('#addpricebtn').click(function() { 



    $('.stidinput').each(function (i) { 

     var inputer = $('.stidinput').val(); 

     $.ajax({ 
      type: 'GET', 
      url: '/Builder/rateretriever', 
      datatype: 'json', 
      data: { id: inputer[i] }, 
      success: function (result) { 
       $('.categoryinput').html(result[i].category + ' - ' + result[i].subcategory + ' - ' + result[i].site); 
       $('.formatinput').html(result[i].format); 
       $('.exclusiveinput').html(result[i].exclusive); 
       $('.cminput').html(result[i].costmethod); 
      } 
     }); 
    }); 
}); 

希望你能帮助:)。

编辑

当然,我有以下的HTML代码:

<div class="io-container"> 
<table id="iolist"> 
    <tr> 
     <th>ID</th> 
     <th>Name:</th> 
     <th>Format:</th> 
     <th>Exclusive:</th> 
     <th>Start:</th> 
     <th>End:</th> 
     <th>Cost Method:</th> 
     <th>Target 1:</th> 
     <th>Target 2:</th> 
     <th>Frequency Cap:</th> 
     <th>RC Price:</th> 
     <th>Impressions:</th> 
     <th>Gross Budget Discount:</th> 
     <th>Net Net Budget</th> 
     <th>Net eCPM</th> 
    </tr> 
    <tr> 
     <td><input class="stidinput" name="stidinput" type="text" /></td> 
     <td class="categoryinput"></td> 
     <td class="formatinput"></td> 
     <td class="exclusiveinput"></td> 
     <td>01/01/2015</td> 
     <td>01/01/2015</td> 
     <td class="cminput"></td> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td class="rcinput"></td> 
     <td class="impsinput"></td> 
     <td></td> 
     <td></td> 
     <td></td> 
    </tr> 

</table> 

<button id="addrowbtn">AddRow</button> 
<button id="addpricebtn">AddPrice</button> 

用户应该能够添加行和填充在细胞与类: stidinput,之后用户点击按钮,该按钮将以JSON格式从数据库中检索费率。我需要找到一种方式,jQuery脚本检索所有STID,并填写在从数据库中获得相应的信息,而不是只有第一个...

感谢,

+1

请问您可以编辑您的问题并清楚解释问题吗? – dario 2014-12-27 17:33:53

+0

'var inputer = this.value;'|| 'data:this.id,'???这是真的不清楚你在问什么... – 2014-12-27 17:38:00

+0

我认为inputer必须定义如下:'var inputer = $(this).val();'和数据以及:data:{id:inputer} ' – Banzay 2014-12-27 17:40:39

回答

0

显然你的错误是你填每个迭代中的第1行TR行。我想你首先要抓住每个stidinput的父行并填写相应的数据。它可能看起来像如下(我认为这并不需要使用索引):

$('#addpricebtn').click(function() { 
    $('.stidinput').each(function (i) { 

     var inputer = $(this).val(); 
     var currentRow = $(this).parent(); 

     $.ajax({ 
      type: 'GET', 
      url: '/Builder/rateretriever', 
      datatype: 'json', 
      data: { id: inputer }, 
      success: function (result) { 
       currentRow.find('.categoryinput').html(result.category + ' - ' + result.subcategory + ' - ' + result.site); 
       currentRow.find('.formatinput').html(result.format); 
       currentRow.find('.exclusiveinput').html(result.exclusive); 
       currentRow.find('.cminput').html(result.costmethod); 
      } 
     }); 
    }); 
}); 

您也可以AJAX调用之前添加if...检查已填充以阻止两行的重复填充的该行和服务器脚本的过度调用。

+0

它没有按照我的意愿工作。但是你帮我弄清了代码并重新思考它。谢谢! – renszzzzzzz 2014-12-27 22:02:06