ถามการหาผลรวม ด้วย ajax จากตารางที่มาจากการ clone ครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ถามการหาผลรวม ด้วย ajax จากตารางที่มาจากการ clone ครับ

ถามการหาผลรวม ด้วย ajax จากตารางที่มาจากการ clone ครับ
ทำใบสั่งซื้อนะครับ จากที่ถามครั้งก่อน  คือว่าตอนนี้ เมื่อใส่ราคา ทุกช่องแล้ว จะหาราคารวม นะครับ  จะเอาค่ามาได้ยังไงนะครับ ในเมื่อ class เป็นชื่อเดียวกันหมดเลย เนื่องจาก clone มา 

ตารางเป็นแบบนี้ครับ แต่ว่า ผม clone เพื่อเพิ่ม หรือ ลบ แถวได้ ต้องเขียน  ยังไง ครับ  ช่วยแนะนำหน่อยครับ  จะทำ  ajax นะครับ เมื่อใส่ราคาแล้ว จะรวมให้เลย ในหน้านั้น

<tr class="firstTr">
   <td ><input class="in-no" type="text" "></td>
   <td ><input class="in-item" type="text" ></td>
   <td ><input class="in-desc" type="text"></td>
   <td ><input class="in-qty"  type="text"></td>
   <td ><input class="in-uom"  type="text"></td>
   <td ><input class="in-price" type="text"></td>
   <td ><input class="in-amount" type="text"></td>
</tr>
<tr class="firstTr">
   <td ><input class="in-no" type="text" "></td>
   <td ><input class="in-item" type="text" ></td>
   <td ><input class="in-desc" type="text"></td>
   <td ><input class="in-qty"  type="text"></td>
   <td ><input class="in-uom"  type="text"></td>
   <td ><input class="in-price" type="text"></td>
   <td ><input class="in-amount" type="text"></td>
</tr>
				
คือปกติถ้า  ชื่อ class ต่างกันผมจะ บวก ได้นะครับ


$( ".in-qty" ).change(function(event) {
  var qty = $('.in-qty').val();
  var price =$('.in-price').first().val();
  var amount = show_qty*show_price;
});
 แนะนำหน่อยครับ ว่าต้องทำยังไง


Wowowow 171.96.166.xxx 11-03-2014 18:55:14

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
ลองอ้างอิง index ของแต่ละตัว 

ตัวอย่าง

in-qty in-price in-amount


โค้ดทดสอบ

<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr class="firstTr">
  <td height="25" align="center" >in-qty</td>
  <td height="25" align="center" >in-price</td>
  <td height="25" align="center" >in-amount</td>
</tr>
<tr class="firstTr">  
   <td ><input  type="text" class="in-qty" value="1"></td>  
   <td ><input type="text" class="in-price" value="12"></td>  
   <td ><input class="in-amount" type="text"></td>  
</tr>  
<tr class="firstTr">  
   <td ><input  type="text" class="in-qty" value="1"></td>  
   <td ><input type="text" class="in-price" value="15"></td>  
   <td ><input class="in-amount" type="text"></td>  
</tr>  
</table>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
<script type="text/javascript">
$(function(){
	$(".in-qty").each(function(i,k){
		$(this).keyup(function(){
			var qty=$(".in-qty").eq(i).val();
			var price=$(".in-price").eq(i).val();
			var amount=qty*price;
			$(".in-amount").eq(i).val(amount);
		});
    });
});
</script>


ninenik 115.67.194.xxx 11-03-2014
 ความคิดเห็นที่ 2
ลืมไป ว่ามีกรณี clone 
ถ้าเป็นกรณี การ clone จะอ้างอิง index เริ่มต้นไม่ได้

ใช้เป็นแบบนี้แทน

ตัวอย่าง

in-qty in-price in-amount


     



โค้ดทดสอบ


<table id="myTbl" width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
  <td height="25" align="center" >in-qty</td>
  <td height="25" align="center" >in-price</td>
  <td height="25" align="center" >in-amount</td>
</tr>
<tr class="firstTr">  
   <td ><input  type="text" class="in-qty" value="1"></td>  
   <td ><input type="text" class="in-price" value="12"></td>  
   <td ><input class="in-amount" type="text"></td>  
</tr>  
<tr class="firstTr">  
   <td ><input  type="text" class="in-qty" value="1"></td>  
   <td ><input type="text" class="in-price" value="15"></td>  
   <td ><input class="in-amount" type="text"></td>  
</tr>  
</table>

<br />
<br />
<table width="500" border="0" cellspacing="0" cellpadding="0">  
  <tr>  
    <td>  
    <button id="addRow" type="button">+</button>    
    &nbsp;  
    <button id="removeRow" type="button">-</button>  
    &nbsp;  
    &nbsp;  
</td>  
  </tr>  
</table>  
<br />


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
<script type="text/javascript">
$(function(){
	
    $("#addRow").click(function(){  
        // ส่วนของการ clone ข้อมูลด้วย jquery clone() ค่า true คือ  
        // การกำหนดให้ ไม่ต้องมีการ ดึงข้อมูลจากค่าเดิมมาใช้งาน  
        // รีเซ้ตเป็นค่าว่าง ถ้ามีข้อมูลอยู่แล้ว ทั้ง select หรือ input  
        $(".firstTr:eq(0)").clone(true)   
        .find("input").attr("value","").end()  
        .find("select").attr("value","").end()  
        .appendTo($("#myTbl"));  
    });  
    $("#removeRow").click(function(){  
        // // ส่วนสำหรับการลบ  
        if($("#myTbl tr").size()>1){ // จะลบรายการได้ อย่างน้อย ต้องมี 1 รายการ  
            $("#myTbl tr:last").remove(); // ลบรายการสุดท้าย  
        }else{  
            // เหลือ 1 รายการลบไม่ได้  
            alert("ต้องมีรายการข้อมูลอย่างน้อย 1 รายการ");  
        }  
    });   
	
	$(".in-qty").click(function(){
		$(this).select();	
	})
	$(".in-qty").keyup(function(){
		var indexObj=$(".in-qty").index(this);
		var qty=$(".in-qty").eq(indexObj).val();
		var price=$(".in-price").eq(indexObj).val();
		var amount=qty*price;
		$(".in-amount").eq(indexObj).val(amount);		
	});

});
</script>


ninenik 124.120.18.xxx 11-03-2014
 ความคิดเห็นที่ 3
 คือ ผมจะเอาค่า  ใน ช่อง  in-amount  มารวมกันทั้งหมดนะครับ  เป็นราคา สุทธิ
เป็นกรณี การ clone  นะครับ 

แต่โค้ด ตัวอย่างเป็น การหาค่า แค่ตัวเดียว ผมลองดันแปลงแล้วยังไม่ได้เลยครับ
 
หน้านี้ หน้าเดียว ผมทำมา 3  วันแล้วครับ  ช่วยแนะนำอีกรอบครับ
ขอบคุณมากครับ


wowowow 171.96.143.xxx 12-03-2014 10:46
 ความคิดเห็นที่ 4
ลองโค้ดด้านล่างไป ประยุกต์ดู

in-qty in-price in-amount
   


     



โค้ดทดสอบ


<table id="myTbl" width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
  <td height="25" align="center" >in-qty</td>
  <td height="25" align="center" >in-price</td>
  <td height="25" align="center" >in-amount</td>
</tr>
<tr class="firstTr">  
   <td ><input  type="text" class="in-qty" value="1"></td>  
   <td ><input type="text" class="in-price" value="12"></td>  
   <td align="right" ><input class="in-amount" type="text"></td>  
</tr>  
<tr class="firstTr">  
   <td ><input  type="text" class="in-qty" value="1"></td>  
   <td ><input type="text" class="in-price" value="15"></td>  
   <td align="right" ><input class="in-amount" type="text"></td>  
</tr>  
</table>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>  
   <td >&nbsp;</td>  
   <td >&nbsp;</td>  
   <td align="right" ><input class="in-amount-total" type="text"></td>  
</tr>  
</table>

<br />
<br />
<table width="500" border="0" cellspacing="0" cellpadding="0">  
  <tr>  
    <td>  
    <button id="addRow" type="button">+</button>    
    &nbsp;  
    <button id="removeRow" type="button">-</button>  
    &nbsp;  
    &nbsp;  
</td>  
  </tr>  
</table>  
<br />


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
<script type="text/javascript">
$(function(){
	
    $("#addRow").click(function(){  
        // ส่วนของการ clone ข้อมูลด้วย jquery clone() ค่า true คือ  
        // การกำหนดให้ ไม่ต้องมีการ ดึงข้อมูลจากค่าเดิมมาใช้งาน  
        // รีเซ้ตเป็นค่าว่าง ถ้ามีข้อมูลอยู่แล้ว ทั้ง select หรือ input  
        $(".firstTr:eq(0)").clone(true)   
        .find("input").attr("value","").end()  
        .find("select").attr("value","").end()  
        .appendTo($("#myTbl"));  
    });  
    $("#removeRow").click(function(){  
        // // ส่วนสำหรับการลบ  
        if($("#myTbl tr").size()>1){ // จะลบรายการได้ อย่างน้อย ต้องมี 1 รายการ  
            $("#myTbl tr:last").remove(); // ลบรายการสุดท้าย  
        }else{  
            // เหลือ 1 รายการลบไม่ได้  
            alert("ต้องมีรายการข้อมูลอย่างน้อย 1 รายการ");  
        }  
    });   
	
	$(".in-qty").click(function(){
		$(this).select();	
	})
	$(".in-qty").keyup(function(){
		var indexObj=$(".in-qty").index(this);
		var qty=$(".in-qty").eq(indexObj).val();
		var price=$(".in-price").eq(indexObj).val();
		var amount=qty*price;
		$(".in-amount").eq(indexObj).val(amount);		
		
		var totalAmount=0;
		$(".in-amount").each(function(i,k){  
			var qty=$(".in-qty").eq(i).val();  
			var price=$(".in-price").eq(i).val();  
			var amount=qty*price;  
			$(".in-amount").eq(i).val(amount);
			totalAmount+=amount;  
		});  	
		$(".in-amount-total").val(totalAmount);		
	});
	
	
	// // ส่วนนี้ เป็นการแสดง รวม ค่าในช่อง in-amount 
	var totalAmount=0;
    $(".in-amount").each(function(i,k){  
		var qty=$(".in-qty").eq(i).val();  
		var price=$(".in-price").eq(i).val();  
		var amount=qty*price;  
		$(".in-amount").eq(i).val(amount);
		totalAmount+=amount;  
    });  	
	$(".in-amount-total").val(totalAmount);
	
	
});
</script>


ninenik 115.67.7.xxx 12-03-2014
 ความคิดเห็นที่ 5
ขอบคุณมากครับ

ทำให้ผมได้เข้าใจ และรู้จัก .each .eq  อีกหน่อยแล้ว 


wowowow 61.90.42.xxx 13-03-2014 09:22
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ