2015-01-15 38 views
0
$.ajax({    
    type: "POST", 
    contentType: 'application/json;charset=utf-8', 
    dataType:'json', 
    url: 'generatequotations', 
    data: JSON.stringify(), 
    success: function(result) { 
    // alert("success"); 
    console.log(result); 
    $.each(result, function(i, item) { 
     tr = $('<tr/>'); 
     tr.append($("<td/>").html('<input type="text" name="unit_price" id="unit_price"/>')); 
     tr.append($("<td/>").html('<input type="text" readonly="true" name="total" id="total"/>')); 
     $('#tbl_items').append(tr); 
    }); 
    } 
}); 

<body> 
<table id="tbl_items"></table> 
</body> 

假设上面的代码会在表中生成10个“unit_price”文本框,我需要的是我想单独获取这10个文本框的值以供进一步计算。帮我怪才......如何分别从动态生成的文本框中获取值?

链接到屏幕截图http://oi62.tinypic.com/255hslc.jpg

两行动态生成的我想每个文本框(“UNIT_PRICE”)内相应的总(“总”)文本框

+0

您需要在输入加载后编写代码,即在追加tr后。 –

+0

追加tr后,我得到了一个动态生成的文本框表。我需要分别获取这些动态文本框的值。 – DON

回答

1

将您的文本框选择器从id更改为class,因为您必须具有多个值。

<input type="text" name="unit_price" class="unit_price"/> 
<input type="text" name="unit_price" class="total"/> 

jQuery的

total = 0; 
$(".unit_price").each(function(){ 
    //Find total column and put unit_price value in total column 
    $(this).closest(".total").val($(this).val()); 
}); 
+0

这项工作?您不要将值从字符串转换为数字。 –

+0

检查我的代码,并将其实现到您的代码http://jsfiddle.net/796k3b9p/2/ – Sadikhasan

+0

感谢花花公子的工作:) :) :) – DON

0

你可以用两种不同的方式的价值。

第一:当你在这时添加一个文本框到行中时,将它的id作为“id”+ i,这会为每个文本框提供不同的id,同样的事情也可以应用到row。第二:如果你为所有文本框(你当前正在做的)提供相同的id,那么你可以使用“this”找到它的值。 $(this).closest('input'),$(this).siblings(),$(this).parent()这些将帮助您获得Textbox的价值。

无论您尝试什么方法,都可以在浏览器的控制台上试用,特别是在Chrome上,这将对您有很大的帮助。

+0

你能给出一个简单的例子,你的第一个解决方案:) – DON

+0

哦,我给你一个小例如对于(i = 0; i <10; i ++){

相关问题