2014-10-01 3 views
0

나는이 JQuery와jQuery 중복 요소를 여러 개 만드는 방법은 무엇입니까?

$('#qty input').on('input', function() { 
       var price = $(this).prev('#price input').val(); 
       var qty = $(this).val(); 
       $('#amount').html(parseFloat(price * qty).toFixed(2)); 
       $('#linetotal input').val(parseFloat(price * qty).toFixed(2)); 
      }); 

의 라인과 HTML을

<tr> 
    <td><select name='itemone[]'> 
     <?php echo $items; ?> 
    </select></td> 
    <td><input name='itemtwo[]' type="text" class="irow"></td> 
    <td id="price"><input name='itemthree[]' type="number" step="any" class="irow" value="0.00"></td> 
    <td id="qty"><input name='itemfour[]' type="number" step="any" class="irow" value="1"></td> 
    <td id="linetotal" class="text-right"><p id="amount">0.00</p> 
     <input name='itemfive[]' type="hidden" class="irow" value="0" > 
    </td> 
</tr> 

는 지난 TD에 표시됩니다 세 번째 TD와 네 번째 TD과 결과의 값을 계산하는 데 무엇을 . 성공했습니다. 그러나 정확한 테이블을 새로운 테이블 데이터로 복사 할 버튼을 클릭하면 동일한 "ID"가 사용됩니다. 지금, 그 행의 계산이 작동하지 않습니다.

두 개 이상의 동일한 ID가 있어도 같은 ID를 사용하여 계산할 수있는 방법입니까?

나는 sibling(), next(), prev()을 시도했지만 사용하지 못했습니다.

나를 도울 수있는 방법.

TIA, 니미츠

편집 :

여기에 성공적인 이미지와 : https://docs.google.com/file/d/0B6f93ZQiD1h_V0dvbThocVdlekU/edit?usp=drivesdk

두 개 이상의 TR (실패) 경우 : https://docs.google.com/file/d/0B6f93ZQiD1h_MmxzYUVLT3JzQWs/edit?usp=drivesdk

답변

1

요소를 타겟팅하기 위해 ID를 사용하는 클래스로 변경하고 싶을 것입니다.

Here은 작동 버전입니다. html은 다소 복잡해 보이지만 추가 된 클래스 속성을 사용하여 반복되는 복사본입니다.

각 행은 총계를 독립적으로 계산합니다.

HTML :

<table> 
<tr> 
    <td> 
     <select name='itemone[]'> 
      <?php echo $items; ?> 
     </select> 
    </td> 
    <td> 
     <input name='itemtwo[]' type="text" class="irow" /> 
    </td> 
    <td id="price" class="price">price 
     <input name='itemthree[]' type="number" step="any" class="irow" value="0.00" /> 
    </td> 
    <td id="qty" class="qty"> 
     <input name='itemfour[]' type="number" step="any" class="irow" value="1" /> 
    </td> 
    <td id="linetotal" class="linetotal text-right"> 
     <p id="amount" class="amount">0.00</p> 
     <input name='itemfive[]' type="hidden" class="irow" value="0" /> 
    </td> 
</tr> 
<tr> 
    <td> 
     <select name='itemone[]'> 
      <?php echo $items; ?> 
     </select> 
    </td> 
    <td> 
     <input name='itemtwo[]' type="text" class="irow" /> 
    </td> 
    <td id="price" class="price">price 
     <input name='itemthree[]' type="number" step="any" class="irow" value="0.00" /> 
    </td> 
    <td id="qty" class="qty"> 
     <input name='itemfour[]' type="number" step="any" class="irow" value="1" /> 
    </td> 
    <td id="linetotal" class="linetotal text-right"> 
     <p id="amount" class="amount">0.00</p> 
     <input name='itemfive[]' type="hidden" class="irow" value="0" /> 
    </td> 
</tr> 

JS :

$(".qty :input").change(function() { 
    var price = $(this).closest('tr').find('.price :input').val(); 
    var qty = $(this).val(); 
    $(this).closest('tr').find('.amount').html(parseFloat(price * qty).toFixed(2)); 
    $(this).closest('tr').find('.linetotal input').val(parseFloat(price * qty).toFixed(2)); 
}); 
+0

Hello @angusf, 코드에 대한 것보다. 잘 작동하지만 편집하는 동안 변경하고 싶었습니다. '$ (". qty : input"). on'으로 변경하려고했지만 작동하지 않습니다. 그걸 도와 줄 수있어? –

+0

@ NimitzEBatiocoJr. .change() 이벤트는이 작업을 수행합니다. 텍스트 상자에 숫자를 입력하는 경우 탭을 클릭하거나 다른 곳을 클릭하여 이벤트를 트리거하거나 .price 입력에 0.0 이외의 값이 있는지 확인하십시오. – angusf

+0

예, 변경되었지만 텍스트 상자의 숫자를 변경하는 동안 출력 값이 변경되면 더 좋을 것입니다. 그래도 코드에 감사드립니다. –

0

나는 100 % 당신이 무엇인지 하려고하는 것. 하지만 이러한 도움이 될 것입니다 생각 :

http://api.jquery.com/first/ http://api.jquery.com/last/

JQuery와 선택은 당신에게 당신이 원하는 것을 줄 수 있도록 첫 번째 또는 마지막 것들의 목록을 반환해야합니다.

+0

상상이나 내가 10 같은 TR을 가지고 ... 그럼 각 행을 계산합니다 밖으로 그림. 그러나 두 개 이상의 동일한 TD가있는 경우 두 번째 TD가 계산을 중지합니다 (두 번째 행) –

관련 문제