[英]how to get value of input type from dynamic table using jquery
I am working on ecommerce website. 我正在电子商务网站上。
My table gets value from session. 我的桌子从会议中获得价值。 It looks like :
看起来像 :
On Checkout I want to get all the values of table. 在结帐时,我想获取表的所有值。
I have used jquery for this. 我已经为此使用了jQuery 。
Here is my code: 这是我的代码:
<script>
$('#chhk').on('click',function(e) {
e.preventDefault();
alert('click');
var table = $("#tbl tbody");
table.find('tr').each(function (i, el) {
var $tds = $(this).find('td'),
product = $tds.eq(2).text(),
price = $tds.eq(3).text(),
quantity = $tds.eq(4).attr('value'),
total = $tds.eq(5).text();
alert('Row ' + (i + 1) + ':\nProduct: ' + product
+ '\nPrice: ' + price
+ '\nQuantity: ' + quantity
+ '\nTotal: ' + total);
});
});
</script>
Here '#chhk' is checkout button id 这里的“ #chhk”是结帐按钮ID
'#tbl' is my table id '#tbl'是我的表ID
I am getting blank value of quantity by this script as it is input field 我正在通过此脚本获取数量的空白值,因为它是输入字段
Any help would be appreciated.. 任何帮助,将不胜感激..
Try replace this: 尝试替换此:
quantity = $tds.eq(4).attr('value');
with: 与:
quantity = $tds.eq(4).val();
As .attr('value') gives the value at the start, while .val() gives current property. 由于.attr('value')会在开始时提供值,而.val()会提供当前属性。
More info: jQuery .val() vs .attr("value") 更多信息: jQuery .val()与.attr(“ value”)
replace this: 替换为:
quantity = $tds.eq(4).attr('value'),
with 与
quantity = $tds.eq(4).find("input").val(),
well I tried to do it separately and am getting expected result. 好吧,我尝试单独进行操作,并获得了预期的结果。
$('#chhk').on('click',function(e) {
e.preventDefault();
alert('click');
var table = $("#tbl tbody");
table.find('tr').each(function (i, el) {
var $tds = $(this).find('td'),
product = $tds.eq(2).text(),
price = $tds.eq(3).text(),
total = $tds.eq(5).text();
var qty = $(this).find('td input:first').val();
alert('Row ' + (i + 1) + ':\nProduct: ' + product
+ '\nPrice: ' + price
+ '\nTotal: ' + total
+'\n Qua:' + qty);
});
});
Have dealed quantity separately. 分别处理了数量 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.