2014-04-12 42 views
0

我有這個表,我用ajax訪問它。我的文本框如何在ajax中包含'id'?

 Name  Qty Price subtotal exchange   
123 Pepsi  2 5.000 10.000 | [txtbox1] | [button] 
221 Toblerone 1 16.000 16.000 | [txtbox2] | [button] 

我想..如果我在txtbox1 = 1鍵入和我點擊按鈕時,數量將是1

此外,在各txtbox具有不同的ID,其標識是它的過期日期。 下面的代碼:

$.ajax({ 
    type:"POST", 
    url:"<?php echo base_url()?>pembelian/cek_notabeli", 
    dataType : 'json', 
    data:{'nonota':nonota} 
    }).done(function(obj){ 
     if(obj != '-'){ 
      var tbl=$("<table/>").attr("class","table"); 
      var head = "<thead></thead><tbody>"; 
      $("#div1").append(tbl); 
      for(var i=0;i<obj.length;i++) 
      { 
       var tr="<tr>"; 
       var td1="<td>"+obj[i]["idbnb"]+"</td>"; 
       var td2="<td>"+obj[i]["nama"]+"</td>"; 
       var td3="<td>"+obj[i]["jumlah_barang"]+"</td>"; 
       var td4="<td>"+obj[i]["hpp"]+"</td>"; 
       var td5="<td>"+obj[i]["subTotal"]+"</td>"; 
       var td6="<td> 
    <input type='text' class='jumtur' name='jumtur'/></td>"; 

       var row = $('.jumtur').attr('id', obj[i]["exp"]); <-- expired date 
       var td7="<td><button class='btn btn-info'>Retur</button></td>"; 
       var hid= "<td> 
    <input type='hidden' class='jumtur1' name='jumtur'/></td>"; 
       var tr2="</tr>"; 

       var id =$('.jumtur1').attr("id",obj[i]["idbarang"]); 

       $(".table").append(tr+td1+td2+td3+td4+td5+td6+hid+td7); 
      } 
      $(".table").append(head); 
     } 

我怎麼能我的文本框,包括AJAX ID?

回答

0

試試這個

var tbl=$("<table/>").attr("class","table"); 
var head = "<thead></thead><tbody>"; 
$("#div1").append(tbl); 
for(var i=0;i<3;i++) 
{ 
    var tr="<tr>"; 
    var td1="<td>Text1</td>"; 
    var td2="<td>Text2</td>"; 
    var td3="<td>Text3</td>"; 
    var td4="<td>Text4</td>"; 
    var td5="<td>Text5</td>"; 
    var rowtxtid = i; 
    var td6="<td><input id="+rowtxtid+" type='text' class='jumtur' name='jumtur'/></td>";     
    var td7="<td><button class='btn btn-info' rel=" + rowtxtid + ">Retur</button></td>"; 
    var hid= "<td><input type='hidden' class='jumtur1' name='jumtur'/></td>"; 
    var tr2="</tr>"; 

    var id =$('.jumtur1').attr("id",(10 + i)); 

    $(".table").append(tr+td1+td2+td3+td4+td5+td6+hid+td7); 
} 
$(".table").append(head); 


$(".btn-info").click(function(){ 
    var temptxt = $(this).attr("rel"); 
    alert($("#" + temptxt).val()); 
}); 

DEMO

0

您可以直接通過IDS的字符串:

if (obj != '-') { 
    var tbl = $("<table/>").attr("class", "table"); 
    var head = "<thead></thead><tbody>"; 
    $("#div1").append(tbl); 
    for (var i = 0; i < obj.length; i++) { 
     var tr = "<tr>"; 
     var td1 = "<td>" + obj[i]["idbnb"] + "</td>"; 
     var td2 = "<td>" + obj[i]["nama"] + "</td>"; 
     var td3 = "<td>" + obj[i]["jumlah_barang"] + "</td>"; 
     var td4 = "<td>" + obj[i]["hpp"] + "</td>"; 
     var td5 = "<td>" + obj[i]["subTotal"] + "</td>"; 
     var td6 = "<td><input type='text' id='" + obj[i]["exp"] + "' class='jumtur' name='jumtur'/></td>"; 
     var td7 = "<td><button class='btn btn-info'>Retur</button></td>"; 
     var hid = "<td><input type='hidden' id='" + obj[i]["idbarang"] + "' class='jumtur1' name='jumtur'/></td>"; 
     var tr2 = "</tr>"; 

     $(".table").append(tr + td1 + td2 + td3 + td4 + td5 + td6 + hid + td7); 
    } 
    $(".table").append(head); 
} 
相關問題