我想在删除按钮点击时更新价格。当我点击添加按钮时,它会添加字段并更新价格。但是在删除按钮上它只是删除字段而价格没有更新。
$(document).ready(function() {
$('#add').on('click', function() {
var maxfld=$('.acmp').length;
var pricedl=500;
if(maxfld<10)
{
$('#new_ac').append('<div id="" class="acmp">'+(++maxfld)+' <p>ACCOMPANYING PERSON DETAILS (No access to Scientific Halls)</p><input type="text" name="name" placeholder="Accompany Full Name" required /><input type="text" name="mob" placeholder="Accompany Mobile Number" required /><br />Gender<br /><input name="gender" type="radio" value="male" required="required" /> Male<input name="gender" type="radio" value="female" required="required" /> Female</div>');
}
if(maxfld>9)
{
alert("Cannot add more accompany");
}
var totalp=(pricedl*maxfld);
$('#amount').html(totalp);
});
$('#del').on('click', function() {
$('.acmp:last-child').remove();
var newp=totalp/maxfld;
$('.price').html(newp);
});
});
答案 0 :(得分:0)
您在删除块中缺少以下变量
var maxfld=$('.acmp').length;
var pricedl=500;
var totalp=(pricedl*maxfld);
可能是以下代码将起作用
$('#del').on('click', function() {
$('.acmp:last-child').remove();
var maxfld=$('.acmp').length;
var pricedl=500;
var totalp=(pricedl*maxfld);
var newp=totalp/maxfld;
$('.price').html(newp);
});
答案 1 :(得分:0)
您需要将变量(totalp,maxfld
)全局化。现在,它们已超出第二次点击功能的范围,它将返回undefined
。您的代码应如下所示:
$(document).ready(function() {
var maxfld = '';
var pricedl = '';
$('#add').on('click', function() {
maxfld = $('.acmp').length;
pricedl = 500;
if (maxfld < 10) {
$('#new_ac').append('<div id="" class="acmp">' + (++maxfld) + ' <p>ACCOMPANYING PERSON DETAILS (No access to Scientific Halls)</p><input type="text" name="name" placeholder="Accompany Full Name" required /><input type="text" name="mob" placeholder="Accompany Mobile Number" required /><br />Gender<br /><input name="gender" type="radio" value="male" required="required" /> Male<input name="gender" type="radio" value="female" required="required" /> Female</div>');
}
if (maxfld > 9) {
alert("Cannot add more accompany");
}
var totalp = (pricedl * maxfld);
$('#amount').html(totalp);
});
$('#del').on('click', function() {
$('.acmp:last-child').remove();
var newp = totalp / maxfld;
$('.price').html(newp);
});
});
答案 2 :(得分:0)
根据我的说法,你需要在获得maxfld
$('#del').on('click', function() {
var pricedl=500,
maxfld=$('.acmp').length,
totalp=(pricedl*maxfld);
$('.acmp:last-child').remove();
var newp=totalp/maxfld;
$('.price').html(newp);
});