我创建了一个披萨形式,用户可以选择披萨,然后根据需要在订单中添加另一个披萨(通过克隆原始披萨形式)。每个披萨的成本出现在表格旁边的h2标题中。
问题:如果用户选择了他们想要的披萨,然后点击添加披萨按钮,则上一披萨的价格会自动放入新的h2标题中(即使用户没有&# 39;然而又选择了第二个比萨饼。)
如何删除新克隆的h2标题中的文本?
HTML:
<div id="pizzaForm">
<fieldset>
<form class="pure-form">
<legend>Pizza</legend>
<label><b>Pizza Type: </b></label>
<select id="pizza">
<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 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: 9cm; border: solid black 2px; padding: 5px"> £0.00 </h2>
<br>
</div>
<div id="extraPizza"></div>
<center><button id="addPizza"> Add Pizza </button></center>
JS:
$(document).on("change","#pizzaSize", function() {
var selectionPrice = $('option:selected', this).attr('data-price');
var selectionInt = parseFloat(selectionPrice, 10);
pizzaCost = selectionInt;
$(this).closest('fieldset').nextAll('h2').first().text("£" + pizzaCost);
});
$( "#addPizza" ).click(function() {
$("#pizzaForm").clone().appendTo("#extraPizza");
});
答案 0 :(得分:1)
h2位于克隆元素中。因此,请使用find()
。
$( "#addPizza" ).click(function() {
var c=$("#pizzaForm").clone();
c.find("h2").html("");
c.appendTo("#extraPizza");
});