2016-12-01 43 views
0

我有一個像下面這樣的jQuery腳本,我想在name='service_id["+kode+"]'上設置kode的遞增數字。問題是數字還沒有遞增,只是顯示1.迭代數在'.each'jquery

如何設置增量行數?

這裏是我的腳本:

function getCorporateService(id){ 
    // get data and parsing to column 
    $.get("{{ url('salesorder/service')}}/"+id, function(data){ 
     console.log(id); 
     console.log(data); 

     var kode = 0; 
     $.each(data, function (index, element){ 
      kode++; 
      $br = "<tr id='item'>"; 
      $br += "<td> <input class='input-small' type='text' id='order_identifier' name='order_identifier' readonly></td>"; 
      $br += "<td><input class='input-small' type='text' id='service_id["+id+"]' name='service_id["+kode+"]' value='"+element.service_name+"' readonly></td>"; 
      $br += "<td><select id='order_type["+id+"]' name='order_type["+id+"]'> <option> - </option> <option value='add'>Add</option> <option value='change'>Change</option> <option value='cancel'>Cancel</option> </select></td>"; 
      $br += "<td><input class='input-small' type='text' id='select_plan["+id+"]' name='select_plan["+kode+"]'></td>"; 
      $br += "<td><input class='input-mini' type='text' id='qty["+id+"]' name='qty["+kode+"]' value='1' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><input class='input-small' type='text' id='unit_price["+id+"]' name='unit_price["+kode+"]' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><input class='input-small' type='text' id='total_price["+id+"]' name='total_price["+kode+"]' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><textarea class='input-small' id='notes["+id+"]' name='notes["+kode+"]'></textarea></td>"; 
      $br += "</tr>"; 

      $(".corporatesvc").append($br); 

     }); 
    }); 
} 
+2

所以,只要使用'index'? – Cerbrus

+1

如果你想讓kode增加,你必須把這個值賦值回kode,就像這個kode = kode + 1一樣,而且你不需要使用一個增量變量,你可以直接使用索引 – GraveyardQueen

+1

@GraveyardQueen:他已經增加了kode ':'kode ++;' – Cerbrus

回答

1

這似乎是與你的JSON數據的問題,您是從響應接收。

見工作實例:

$(document).ready(function() { 
 
    var data = [{ 
 
    "service_name": "Test 1", 
 
    "Major": "Physics" 
 
    }, { 
 
    "service_name": "Test 2", 
 
    "Major": "Chemistry" 
 
    }, { 
 
    "service_name": "Test 3", 
 
    "Major": "Mathematics" 
 
    }]; 
 
    var id = 1; 
 
    var kode = 0; 
 
    $.each(data, function(index, element) { 
 
    kode++; 
 
    $br = "<tr id='item'>"; 
 
    $br += "<td> <input class='input-small' type='text' id='order_identifier' name='order_identifier' readonly></td>"; 
 
    $br += "<td><input class='input-small' type='text' id='service_id[" + id + "]' name='service_id[" + kode + "]' value='" + element.service_name + "' readonly></td>"; 
 
    $br += "<td><select id='order_type[" + id + "]' name='order_type[" + id + "]'> <option> - </option> <option value='add'>Add</option> <option value='change'>Change</option> <option value='cancel'>Cancel</option> </select></td>"; 
 
    $br += "<td><input class='input-small' type='text' id='select_plan[" + id + "]' name='select_plan[" + kode + "]'></td>"; 
 
    $br += "<td><input class='input-mini' type='text' id='qty[" + id + "]' name='qty[" + kode + "]' value='1' onChange='getTotalPrice(" + id + ")'></td>"; 
 
    $br += "<td><input class='input-small' type='text' id='unit_price[" + id + "]' name='unit_price[" + kode + "]' onChange='getTotalPrice(" + id + ")'></td>"; 
 
    $br += "<td><input class='input-small' type='text' id='total_price[" + id + "]' name='total_price[" + kode + "]' onChange='getTotalPrice(" + id + ")'></td>"; 
 
    $br += "<td><textarea class='input-small' id='notes[" + id + "]' name='notes[" + kode + "]'></textarea></td>"; 
 
    $br += "</tr>"; 
 

 
    $(".corporatesvc").append($br); 
 

 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="corporatesvc"> 
 

 
</div>

見代碼片段中的jsfiddle:https://jsfiddle.net/z6gbcutg/2/