선택한 제품에 따라 단락의 값을 동적으로 변경하려면 어떻게해야합니까?
내 코드에서 이것이 일어나는 일입니다. MacBook
수량을 선택 하고 입력하면 실제 총 가격을 확인할 수 있지만 다른 제품을 선택하면 Total
이미 선택한 모든 제품에 대해 전역 적으로 변경 됩니다.
Item: MacBook
Amount : 8900
Qty : 1
Total : 1200
Item : HP Probook
Amount : 1200
Quantity: 1
Total: 1200
JS
function OptionsSelected(product)
{
$('.container').append(
'<div class="product">'+
'<input type="hidden" value='+product.id+' name="product[] />'+
'<p class="name"> Item: ' + product.name + '</p>'+
'<p class="price" data-price="'+product.value+'">Price : ' + product.value + '</p>'+
'<input type="text" class="quantity" name="quantity" />'+
'<p class="total">Total $:<span></span></p>'+
'</div>'
).appendTo('form')
$('.container').on('keyup','.quantity',function()
{
var a = Number($(this).val());
var b = Number($(this).closest('div').find('.price').data('price'));
c = a * b;
//debugger --breakpoint
alert('Debugging prices for items selected ' +b);
$(".total span").text(c);
});
HTML
<input onclick="return OptionsSelected(this)" type="checkbox" id="{!! $product->id !!}" name="{!! $product->name !!}" value="{!! $product->price !!}" />
<div class="container" id="container">
</div>
추신 : 나는 작동하는 스 니펫을 만들려고했지만 그 작동을 얻을 수 없었고 이것은 이미 질문에 대한 새로운 스레드입니다.
이 라인
$(".total span").text(c);
클래스가있는 모든 요소의 내용을 변경합니다. total
this
(정확한 수량을 얻기 위해 이미 수행 한대로) 사용 .total
하여 변경된 제품 만 변경하십시오.
$('.container').on('keyup', '.quantity', function () {
var quantityInput = $(this);
var product = quantityInput.closest('div');
var quantity = Number(quantityInput.val());
var price = Number(product.find('.price').data('price'));
product.find(".total span").text(quantity * price);
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다