我创建了一个披萨形式,用户可以选择披萨,然后根据需要在订单中添加另一个披萨(通过克隆原始披萨形式)。
我希望用户能够在他们希望的情况下删除Pizza Form,但是removePizza
函数的行为并不像我想要的那样(即它不会删除最接近的.pizzaForm
)
HTML:
<div id="1" class="pizzaForm">
<fieldset>
<form class="pure-form">
<legend>Pizza</legend>
<label><b>Pizza Type: </b></label>
<select id="pizza">
<option>Please Select:</option></option>
<option name="margarita">Margarita</option>
<option name="deep-pan">Deep Pan</option>
<option name="stuffed-crust">Stuffed Crust</option>
</select>
<span style="float:right">
<label><b>Pizza Size: </b></label>
<select id="pizzaSize">
<option data-price="0">Please Select:</option></option>
<option name="e-small" data-price="4.99">Extra Small - £4.99</option>
<option name="small" data-price="5.99">Small - £5.99</option>
<option name="medium" data-price="6.99">Medium - £6.99</option>
<option name="large" data-price="8.99">Large - £8.99</option>
<option name="e-large" data-price="9.99">Extra Large - £9.99</option>
<option name="f-size" data-price="10.99">Family Size - £10.99</option>
</select>
</span>
</form>
</fieldset>
<fieldset style = "border-top:0px">
<form class="pure-form">
<legend><b>Toppings (99p Each): </b></legend>
<input type="checkbox" name="onions">Onions</input>
<input type="checkbox" name="mushrooms">Mushrooms</input>
<input type="checkbox" name="peppers">Peppers</input>
<input type="checkbox" name="olives">Olives</input>
<input type="checkbox" name="garlic"> Garlic</input>
<input type="checkbox" name="peperoni">Peperoni</input>
<input type="checkbox" name="cheese">Pesto</input>
</form>
</fieldset>
<h2 style= "float:left; margin-top:-3cm; margin-left: 8cm; border: solid black 2px; padding: 5px; width:2cm; text-align:center"> £0.00 </h2>
<button class="removePizza" style= "float:left; margin-top:-1.5cm; margin-left: 7.8cm;">Remove Pizza</button>
<br>
</div>
<div id="extraPizza"></div>
<center><button id="addPizza"> Add Pizza </button></center>
JavaScript的:
var pizzaArray = new Array();
function pizza(number, pizzaCost, toppingCost) {
this.pizzaNumber = number;
this.pizzaCost = pizzaCost;
this.toppingCost = toppingCost;
}
var pizzaCounter = 1;
pizzaArray.push(new pizza(pizzaCounter, 0.00, 0.00));
$("#pizza").change(function() {
$("#pizzaSize").prop('disabled', false);
})
$(document).on("change","#pizzaSize", function() {
var formID = $(this).closest('div').attr("id");
for(var i = 0; i < pizzaArray.length; i++) {
if (pizzaArray[i].pizzaNumber == formID) {
var selectionPrice = $('option:selected', this).attr('data-price');
var selectionInt = parseFloat(selectionPrice, 10);
pizzaArray[i].pizzaCost = selectionInt;
calculateCost();
}
}
});
$(document).on("change","input[type='checkbox']", function() {
var checked = $(this).parent().find(":checkbox:checked").length;
var toppingCost = (0.99 * checked);
var formID = $(this).closest('div').attr("id");
for(var i = 0; i < pizzaArray.length; i++) {
if (pizzaArray[i].pizzaNumber == formID) {
pizzaArray[i].toppingCost = toppingCost;
calculateCost();
}
}
});
$( "#addPizza" ).click(function() {
pizzaCounter++;
pizzaArray.push(new pizza(pizzaCounter, 0.00, 0.00));
$("#1").clone().prop("id", pizzaCounter).appendTo("#extraPizza");
$("#"+pizzaCounter).find("input[type='checkbox']").removeAttr('checked');
$("#"+pizzaCounter).find("h2").removeAttr('checked').html("£0.00");
});
$(".removePizza").click(function() {
var numOfForms = $( ".pizzaForm" ).length;
if (numOfForms > 1) {
$(this).closest('.pizzaForm').remove();
} else {
alert ("Cannot delete this form!");
}
});
答案 0 :(得分:2)
您的问题是在将来收听removePizza点击事件,不仅针对当前代码,还针对将来添加的元素。
所以,替换你的:
$(".removePizza").click(function() {
使用:
$(document).on('click', ".removePizza", function() {
所以在appendTo之后所有新的披萨删除按钮将继续有效!
答案 1 :(得分:0)
试试这个,
$("body").on('click','.removePizza',function() {
var numOfForms = $(this).parents('.pizzaForm').length;
if (numOfForms >= 1) {
$(this).parents('.pizzaForm').remove();
} else {
alert ("Cannot delete this form!");
}
});
答案 2 :(得分:0)
在$(“#addPizza”)。click()函数中,需要按类名而不是ID查找要克隆的表单。这是因为您之前可能已使用该ID删除了表单。并在克隆函数中传递TRUE,以便事件也被克隆。我已注释掉你的行并在下面添加了修改后的行。 这是一个有效的jsFiddle: https://jsfiddle.net/ashekthegreat/qt0cgvy2/
$("#addPizza").click(function() {
pizzaCounter++;
pizzaArray.push(new pizza(pizzaCounter, 0.00, 0.00));
// $("#1").clone().prop("id", pizzaCounter).appendTo("#extraPizza");
$(".pizzaForm:first").clone(true).prop("id", pizzaCounter).appendTo("#extraPizza");
$("#" + pizzaCounter).find("input[type='checkbox']").removeAttr('checked');
$("#" + pizzaCounter).find("h2").removeAttr('checked').html("£0.00");
});
答案 3 :(得分:-1)
由于表单是.removePizza按钮的兄弟,因此您需要使用prevAll()来选择第一个/最近的上一个兄弟,例如:
$(this).prevAll('.pizzaForm:first').remove();
如果这不起作用,请提供代码的jsFiddle。