2012-05-06 3 views
0

전자 상거래 사이트가 있습니다. 주 제품에 다양한 변형과 ​​추가 기능을 선택할 수있는 제품 페이지가 있습니다. 고객이 옵션을 클릭 할 때 가격을 동적으로 표시하고 싶습니다.jQuery : 클릭시 속성 값을 더함

기본 HTML은 다음과 같습니다 카트 기능을 추가하고 난 분명 아무것도 그 가격이 고도로 설정되어 변경할 수 있지만 수 있고 제목이 jQuery를 클릭 목표를 설정하기위한

<div class="addons"> 
    <input type="radio" name="products[39][addons][Handles]" title="Handles" value="579" alt="7.00" class="options"> Bronze Left 
    <input type="radio" name="products[39][addons][Handles]" title="Handles" value="580" alt="8.00" class="options"> Bronze Right 
    <input type="radio" name="products[39][addons][Handles]" title="Handles" value="581" alt="9.00" class="options"> Chrome Left 
    <input type="radio" name="products[39][addons][Handles]" title="Handles" value="582" alt="10.00" class="options"> Chrome Right 

    <input type="radio" name="products[39][addons][Glass]" title="Glass" value="589" alt="18.00" class="options"> Brass Clarity 
    <input type="radio" name="products[39][addons][Glass]" title="Glass" value="590" alt="20.00" class="options"> Zinc Abstract 
    <input type="radio" name="products[39][addons][Glass]" title="Glass" value="591" alt="21.00" class="options"> Zinc Elegance 
    <input type="radio" name="products[39][addons][Glass]" title="Glass" value="592" alt="23.00" class="options"> Zinc Prairie 
</div> 

값 속성이 사용됩니다.

jQuery와 관련하여 도움이 필요합니다. 내가 가지고있는 문제는 var 가격을 클릭 기능 외부의 서로에게 더할 수있는 방법을 모른다는 것입니다. 내가 이해하고있어 경우

$("input[type='radio'][title='Handles']").click(function() { 
    price = $(this).val(); 
} 

$("input[type='radio'][title='Glass']").click(function() { 
    price = $(this).val(); 
} 
+2

내가 SO 러시아 억양 내 머리 ...에서 – Hubro

+1

지금까지 @Codemonkey에 질문을 읽어 본 적이있다 : 여기서 그 일을 한 가지 방법이다. 왜 지금부터 시작 했습니까? \ 왜 시작 했습니까 ** 지금 **? – gdoron

+2

@Codemonkey 나는 러시아어가 아니지만 당신이 행복하기 때문에 기뻐요 :) – Chriser

답변

0

정확하게 이해하고 있으면 선택한 각 옵션의 가격을 합산하려고합니다.

// An object to keep track of the total amount of each title 
var total = {}; 

// Change event on the radio buttons 
$('input[type=radio]').change(function() { 
    // Assign the alt to the total object with the key name being the title 
    total[$(this).attr('title')] = parseFloat($(this).attr('alt')).toFixed(2); 

    // Output the result to HTML (optional) 
    outputTotal(); 
}); 

function outputTotal() 
{ 
    // Here all the prices are being summed 
    var sum = 0; 

    for(var index in total) 
    { 
     sum += total[index]; 
    } 

    // And output to HTML 
    $('.total').html(sum); 
} 

jsFiddle example

+0

대단히 감사합니다 !! – Chriser

+0

여기에 문서화 된 parseFloat에 약간의 문제가 있습니다. [link] (http://www.php.net/manual/en/language.types.float.php) 때로는 예 : 3.99999999999가 잘못 표시되는 경우가 있습니다. 내가 찾은 빠른 수정은 합 변수에 .toFixed (2)를 추가하는 것입니다. – Chriser

+0

사실입니다. 나중에 참조 용으로 답변에 추가했습니다. – Hubro

1

, 당신은 단지 고도의 값을 추가 할.

var total = 0; 
$(".addons").find("input:checked").each(function() { 
    total += parseFloat($(this).attr('alt')); 
}); 

당신은 클릭 처리기에서이 코드를 실행하는 것, 및 업데이트 어떤 HTML은 전체가 표시됩니다 : 당신은 다음과 같은 것을 할 수 있습니다.

EDIT "input : checked"는 "input [checked = checked]"보다 깔끔합니다.

+0

대신': checked' 선택자를 사용하는 것이 좋습니다. –

+0

@JustinY 좋은 전화, 고침. – McGarnagle

+0

올바른 동작으로 생각하지 않는 클릭당 가치가 증가합니다. 나는 그것이 좋은 접근이라고 생각했기 때문에 약간 버전을 수정했습니다 ^^ –

1

input[type='radio']:checked을 사용하면 찾고있는 요소를 필터링하고 가져올 수 있습니다.

var total = 0; 

// Get all the input tags that are checked. 
$(".addons").find("input[type='radio']:checked").each(function() { 
    total += parseFloat($(this).attr('alt')); 
}); 
0
var inputs = $(".addons input.options"); 
inputs.click(function() { 
    var total = 0; 
    $(".addons").find(":checked").each(function(){ 
     total += parseFloat($(this).attr("alt")); 
    }); 
    $("#total").val(total); 
}); 

http://jsfiddle.net/tive/fyZbV/

+0

parseFloat에 약간의 문제가 있습니다. [link] (http://www.php.net/manual/en/language.types.float.php) 때로는 예 : 3.99999999999가 잘못 표시됩니다. 내가 찾은 빠른 수정은 total 변수에 .toFixed (2)를 추가하는 것입니다. – Chriser

+0

흠, 나는 .toFixed가 [이전 질문]에서 올바르게 작동하지 않는다는 것을 알았습니다 (http://stackoverflow.com/questions/8505348/change-default-figures-in-textbox-via-dropdown/8642557#8642557).) –

+0

나는 그렇게 해왔다. 그리고 그것은 작동한다 : $ ('. total') .html (sum.toFixed (2)); – Chriser