[英]Removing the closest form on click
I have created a pizza form where the user can select a pizza and then add another pizza to the the order if they wish (by cloning the original pizza form). 我创建了一个比萨饼表单,用户可以选择一个比萨饼,然后根据需要添加另一个比萨饼(通过克隆原始的比萨饼表单)。
I would like the user to be able to remove a Pizza Form if they wish, however the removePizza
function does not behave in the way I would like (ie it does not remove the closest .pizzaForm
) 我希望用户能够根据需要删除Pizza Form,但是removePizza
函数不会按照我想要的方式运行(即,它不会删除最近的.pizzaForm
)
HTML: 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: 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!");
}
});
Your problem is to listen for removePizza click events in future, not only for the current tags but also for future added elements. 您的问题是将来会监听removePizza单击事件,不仅监听当前标签,而且监听未来添加的元素。
So, substitute your: 因此,请替换为:
$(".removePizza").click(function() {
with: 与:
$(document).on('click', ".removePizza", function() {
So after appendTo all new pizza remove buttons will continue to works! 因此,appendTo之后,所有新的披萨移除按钮将继续起作用!
try this, 尝试这个,
$("body").on('click','.removePizza',function() {
var numOfForms = $(this).parents('.pizzaForm').length;
if (numOfForms >= 1) {
$(this).parents('.pizzaForm').remove();
} else {
alert ("Cannot delete this form!");
}
});
In your $("#addPizza").click() function, the form you are cloning needs to be looked up by class name instead of ID. 在$(“#addPizza”)。click()函数中,要克隆的表单需要通过类名而不是ID查找。 This is because You may have previously removed the form with that ID. 这是因为您以前可能已经删除了具有该ID的表单。 And pass TRUE inside the clone function so that events get cloned too. 并在clone函数中传递TRUE,以便事件也可以被克隆。 I have commented out your line and added the modified line below. 我已注释掉您的行,并在下面添加了修改后的行。 Here is a working jsFiddle: https://jsfiddle.net/ashekthegreat/qt0cgvy2/ 这是一个工作的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");
});
Since the form is a sibling to your .removePizza button, you will need to use prevAll() to select the first/most recent previous sibling, for example: 由于表单是您的.removePizza按钮的同级对象,因此您将需要使用prevAll()选择前一个/最近的同级对象,例如:
$(this).prevAll('.pizzaForm:first').remove();
If that doesn't work, please provide a jsFiddle of your code. 如果那行不通,请提供代码的jsFiddle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.