简体   繁体   English

jQuery / JS表单问题

[英]jQuery / JS form issue

I have a HTML form which I need help with adding and removing data using jQuery (or JavaScript). 我有一个HTML表单,我需要使用jQuery(或JavaScript)添加和删除数据的帮助。

 $('.purchase-car').on('click', 'button', function(event) { event.preventDefault(); /* Act on the event */ var carName = $('.purchase-car select').val(); var carPrice = $('.purchase-car select').find(':selected').data('price'); var carQuantity = $('.purchase-car input[name="quantity"]').val(); var totalPrice = carPrice * carQuantity; var orderToAdd = '<li>' + '<span>' + carName + ' (' + carQuantity + ')</span> <span>remove</span>' + '</li>'; $('ul').prepend(orderToAdd); $('#payment-amount').html(total); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li>volvo (1) <span>remove</span> </li> <li>mercedes (2) <span>remove</span> </li> </ul> <p>Total Price: <span id="payment-amount">30</span> </p> <div class="purchase-car"> <select> <option value="volvo" data-price="10">Volvo ( $10 )</option> <option value="saab" data-price="20">Saab ( $20 )</option> <option value="mercedes" data-price="10">Mercedes ( $10 )</option> <option value="audi" data-price="30">Audi ( $30 )</option> </select> <input type="number" name="quantity" value="1"> <button type="submit">Add</button> </div> 

There will be one select field and one input field, once I click add (on submit) both of the values should be added in a ul as list item. 将有一个选择字段和一个输入字段,一旦我单击“添加”(在提交时),两个值都应作为列表项添加到ul中。 I have successfully done so. 我已经成功做到了。 But I need a calculation here. 但是我需要在这里计算。

Like, what's the total price of selected objects. 就像,选定对象的总价是多少。 Also It should subtract from total price when I remove particular object. 当我删除特定对象时,也应该从总价中减去。

http://jsfiddle.net/getanwar/s7ob01o1/ http://jsfiddle.net/getanwar/s7ob01o1/

Update: I don't want to insert those data directly to the DOM. 更新:我不想将那些数据直接插入DOM。 Because this is a part of another form and I don't want this data to be editable from console or via developer tools. 因为这是另一种形式的一部分,所以我不希望通过控制台或通过开发人员工具来编辑此数据。 So if I could store these data into an array or an object and calculate from there would be really helpful. 因此,如果我可以将这些数据存储到数组或对象中并从那里进行计算,将非常有帮助。

Taking your update into consideration, you should create a global variable. 考虑到您的更新,您应该创建一个全局变量。

var vehicles = new Array;

To increase the value, in the function where you insert the vehicle add the line: 要增加该值,请在插入车辆的函数中添加以下行:

var currentValue = parseInt($("#payment-amount").html());
$("#payment-amount").html(curentValue + (carPrice * carQuantity));
vehicles.push({price: carPrice, quantity: carQuantity});

Then to update the price when you remove one: 然后在您删除价格时更新价格:

$("ul").on("click", "li span", function() {
    var index = $(this).parent().index();
    var currentValue = parseInt($("#payment-amount").html());
    var removeValue = vehicles[index]['price'] * vehicles[index]['quantity'];
    $("#payment-amount").html(currentValue - removeValue);
    vehicles.splice(index, 1);
    $(this).parent().remove();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM