表的id=“extra_table”;
row 是点击产生的行索引 我设了全局变量;
我每一行都是动态生成的,每个单元格都有一个文本框。
我现在都是$("#extra_table").find("tr").eq(row).find("td").eq(1).children().val("hello");
这样能拿到文本框并赋值;
我就想问能不能
$("#extra_table").find("tr").eq(row).find("input[type='text']").eq(1).val();
这样拿到的都是空白
eq() 选择器选取带有指定 index 值的元素。
index 值从 0 开始,所有第一个元素的 index 值是 0(不是 1)。
你的dom元素能贴出来看看嘛
var index=$("#extra_table tr").length;
$("#extra_table").append("<tr align='center'>" +
"<td>"+(index)+"</td>" +
"<td><input type='text' size='15' name='productcode' readonly='true' ><img name='products' style='cursor:pointer' src='images/selectDate.gif'></td>" +
"<td><input size='15' type='text' readonly='true' name='name' ></td>" +
"<td><input size='15' type='text' readonly='true' name='unitname' ></td>" +
"<td><input size='15' type='text' readonly='true' name='unitprice' ></td>" +
"<td><input size='15' type='text' name='num' ></td>"+
"<td><input size='15' type='text' readonly='true' name='itemprice' ></td>" +
"<td><img src='images/delete.gif' name='delete' style='cursor:pointer'></td>" +
"</tr>");
});
$("#extra_table input[name=num]").live("change",function(){
var price=$(this).parent().parent().children().eq(4).children().eq(0).val();
var num=$(this).val();
alert("price="+price+"/num="+num);
var itemprice=price*num;
$(this).parent().parent().children().eq(6).children().eq(0).val(itemprice);
});
var trindex=0;
var choseindex=0;
$("img[name='products']").live("click",function(){
trindex=$(this).parent().parent().index();
$(".show_detailpuchase_div").hide();
$("#productDiv").show();
});
$("#product_reset").click(function(){
$("#productDiv").hide();
$(".show_detailpuchase_div").show();
});
$("#product_sure").live("click",function(){
var row=trindex;
var id=$(".product_tr").eq(choseindex).children().eq(1).text();
var name=$(".product_tr").eq(choseindex).children().eq(2).text();
var uname=$(".product_tr").eq(choseindex).children().eq(3).text();
var price=$(".product_tr").eq(choseindex).children().eq(4).text();
$("#extra_table").find("tr").eq(row).find("td").eq(1).children().val(id);
$("#extra_table").find("tr").eq(row).find("td").eq(2).children().val(name);
$("#extra_table").find("tr").eq(row).find("td").eq(3).children().val(uname);
$("#extra_table").find("tr").eq(row).find("td").eq(4).children().val(price);
$("#productDiv").hide();
$(".show_detailpuchase_div").show();
});
$("#extra_table").find("tr").eq(row).find("input[type='text']").eq(1).val();
你这句是打印出来了吗,如下:
console.log($("#extra_table").find("tr").eq(row).find("input[type='text']").eq(1).val());
另外建议你用一个变量存起来,提高效率:
var dom = $("#extra_table").find("tr").eq(row).find("td");
dom.eq(1).children().val(id);
dom.eq(2)......
dom.eq(3)......
...
$("#extra_table").find("tr").eq(row).find("input[type=text]").eq(1).val();//把引号去了