2012-03-30 2 views
0

문제가 있습니다. Zend 프레임 워크를 기반으로하는 응용 프로그램에는 장바구니 (표로 표시)가 있습니다. 모든 행은 상점 바구니에있는 제품입니다. 모든 행에는 두 개의 양식 필드 (수량 및 가격 (= 가격 * 수량))가 있습니다.JavaScript 다중 입력 onChange

나는 자바 스크립트와 onChange 이벤트와 수량 (가격 * 수량)에 의해 값을 변경하려면,하지만 난 1. 입력이있는 foreach 루프에 제시되어있다> 행 수와 함께이 작업을 수행하는 방법을 모른다 젠드 뷰.

JavaScript를 보완하기 위해 범용 알고리즘이 있습니까?

+0

은 당신이 지금까지 가지고 않습니다은 diff TR에서 동일한 TD를받을 수 있나요? – bernie

답변

0

각 값 필드는 예를 들어? 수량 필드의 ID에서 파생 될 수있는 ID 속성? 그렇다면 매우 빠르고 고통 스럽습니다. 주어진 :

<table id="basket"> 
    <tr class="basket-row"> 
     <td class="basket-quantity"> 
      <input type="text" id="product1234_quantity"> 
     </td> 
     <td class="basket-subtotal"> 
      <input type="hidden" id="product1234_unitprice" value="20"> 
      <input type="text" readonly="readonly" id="product1234_subtotal"> 
     </td> 
    </tr> 
    <tr class="basket-row"> 
     <td class="basket-quantity"> 
      <input type="text" id="product5678_quantity"> 
     </td> 
     <td class="basket-subtotal"> 
      <input type="hidden" id="product5678_unitprice" value="30"> 
      <input type="text" readonly="readonly" id="product5678_subtotal"> 
     </td> 
    </tr> 
</table> 

각 제품의 단가는 숨겨진 속성이지만 여기에 장바구니가 저장되는 위치는 나와 있습니다. 어딘가에서 얻을 수 있다고 가정하고 jQuery를 가정 해 봅시다. 다른 아이디에서 ID를 추출 할 수없는 경우

$('#basket').on('change','.basket-quantity input',function(e) { 
    $('#'+this.id.replace('_quantity','_subtotal')).val(Number($('#'this.id.replace('_quantity','_unitprice')))*Number(this.value)); 
}); 

, 당신은 좀 더 복잡한 DOM의 질의를해야 할 것입니다,하지만 포인트는 핸들러의 런타임에, 당신은 어떤 입력을 '파악해야 다시 업데이트.

사용중인 프레임 워크 (있는 경우)를 지정하지 않았습니다. 평범한 JS에서는이 기능을 통해 브라우저 간 호환이 가능하고 이벤트 첨부 및 처리를 표준화 할 수있는 많은 추가 코드가 필요합니다. 각각의

+0

위 .. 괜찮아 보이는 군, 나는 그것을 시험하려고 노력할 것이다. – dariusz

0

사용할 수

$(this).index() 

예컨대

$("#jieguo_qian table tr:nth-child(4) td").each(function(){ 
    var tindex = $(this).index() + 1; 

그래서 당신은

th th th 
td td td 
td td td