繁体   English   中英

使用JavaScript格式化附加文本

[英]Formatting appended text using JavaScript

HTML代码

<button id="btn1">show item no. 1</button>
<button id="btn2">show item no.2</button>
<div id="resulttext"></div>

JavaScript代码

price1=20;
price2=50;
qty1=0;
qty2=0;
amount=0;

$(document).ready(function(){
    $('#btn1').click(function(){
        qty1=qty1 + 1;
        amount=price1*qty1;
        $("resultqty").change(qty1);
        $("resultprice").change(amount);
        $("#resulttext").append("<div><button type='button' id='remove1' class='close pull-right' aria-hidden='true'>&times;</button>REGULAR BURGER" + "<p> Quantity:"+qty1+"</p><p> Unit Total:"+amount+"</div>");
    })
})
$(document).on('click', 'button#remove1', function(){        
     $(this).parent().remove();
});

$(document).ready(function(){
    $('#btn2').click(function(){
        qty2=qty2 + 1;
        amount=price2*qty2;
        $("resultqty").change(qty2);
        $("resultprice").change(amount);
        $("#resulttext").append("<div><button type='button' id='remove2' class='close pull-right' aria-hidden='true'>&times;</button>SPECIAL BURGER"+ "<p> Quantity:"+qty2+"</p><p> Unit Total:"+amount+"</div>");
    })
})
$(document).on('click', 'button#remove2', function(){        
    $(this).parent().remove();
});

上面的代码实际上是有效的,但是我的主要问题是,当我单击一个特定的按钮时,必须添加的文本应该只出现一次,但是当您连续单击同一按钮时,数量和数量仍应实时更改。 。

另一件事是,我想获取两个按钮的总和并将其显示在页面上。

编辑:

这是HTML:

<button id="btn1">show item no. 1</button>
<button id="btn2">show item no.2</button>
<div id="resulttext"></div>

这里的js:

price1=20;
price2=50;
qty1=0;
qty2=0;
amount=0;

$(document).ready(function(){
    $('#btn1').click(function(){

        qty1=qty1 + 1;
        amount=price1*qty1;
        $("resultqty").change(qty1);
        $("resultprice").change(amount);
        if($("#part1").length>0){
            $("#part1").html("<div><button type='button' id='remove1' class='close pull-right' aria-hidden='true'>&times;</button>REGULAR BURGER" + "<p> Quantity:"+qty1+"</p><p> Unit Total:"+amount+"</div>");
        }else{
            $("#resulttext").append("<div id='part1'><div><button type='button' id='remove1' class='close pull-right' aria-hidden='true'>&times;</button>REGULAR BURGER" + "<p> Quantity:"+qty1+"</p><p> Unit Total:"+amount+"</div></div>");
        }
    })
})
$(document).on('click', 'button#remove1', function(){        
    $("#part1").html("");
});


$(document).ready(function(){
    $('#btn2').click(function(){
        qty2=qty2 + 1;
        amount=price2*qty2;
        $("resultqty").change(qty2);
        $("resultprice").change(amount);
        if($("#part2").length>0){
            $("#part2").html("<div><button type='button' id='remove2' class='close pull-right' aria-hidden='true'>&times;</button>SPECIAL BURGER"+ "<p> Quantity:"+qty2+"</p><p> Unit Total:"+amount+"</div>");
        }else{
            $("#resulttext").append("<div id='part2'><div><button type='button' id='remove2' class='close pull-right' aria-hidden='true'>&times;</button>SPECIAL BURGER"+ "<p> Quantity:"+qty2+"</p><p> Unit Total:"+amount+"</div></div>");
        }
    })
});
$(document).on('click', 'button#remove2', function(){        
    $("#part2").html("");
});

在这里摆弄: http : //jsfiddle.net/YLEXU/3/

将.append()更改为.html()并使用另一个ID创建新的div。

jsfiddle.net/wjkH4/

暂无
暂无

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

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