我有这个表,我用ajax访问它。
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,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 :(得分: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());
});
答案 1 :(得分:0)
您可以直接在字符串上传递ID:
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);
}