Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery向跨度元素添加数值_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery向跨度元素添加数值

Javascript jQuery向跨度元素添加数值,javascript,jquery,html,Javascript,Jquery,Html,我有一个HTML span元素,如下所示 <span id="total_cost">4.99</span> <script type="text/javascript"> $('input[name="delivery_method"]').on('change', function(){ if ($(this).val()=='signed') { $("#delivery_price").html("&pound;2.0

我有一个HTML span元素,如下所示

<span id="total_cost">4.99</span>
<script type="text/javascript">
$('input[name="delivery_method"]').on('change', function(){
    if ($(this).val()=='signed') {
        $("#delivery_price").html("&pound;2.00");
        $("#delivery_method").html("Royal Mail Signed For First Class");
        $("#total_cost").html("&pound;<?php echo ($item)*4.99 + 2.00; ?>");
    } else if($(this).val()=='guaranteed')  {
        $("#delivery_price").html("&pound;6.22");
        $("#delivery_method").html("Royal Mail Special Delivery Guaranteed by 1PM");
        $("#total_cost").html("&pound;<?php echo ($item)*4.99 + 6.22; ?>");
    } else {
        $("#delivery_price").html("&pound;0.00");
        $("#delivery_method").html("Royal Mail First Class");
        $("#total_cost").html("&pound;<?php echo ($item)*4.99; ?>");
    }
});
</script>
任何帮助都将不胜感激。 非常感谢

试试看

<span id="total_cost">4.99</span>

<!-- radio field -->
<input type="radio" name="delivery_method" value="standard" data-price="0.00" data-method="Royal Mail First Class" checked />standard (FREE)
<input type="radio" name="delivery_method" value="signed" data-price="2.00" data-method="Royal Mail Signed For First Class" />Signed (2.00)
<input type="radio" name="delivery_method" value="guaranteed" data-price="6.22" data-method="Royal Mail Special Delivery Guaranteed by 1PM" />Guaranteed (6.22)
<!-- checkbox -->
<input type="checkbox" value="digital" name="digital" />Digital copy (2.00)

演示:

您没有为复选框显示任何代码,因此我不确定您尝试了什么,或者失败的原因,或者您正试图对其执行什么操作,但是如果您只需要复选框的值

给复选框一个ID

 <input type="checkbox" ID='digital' value="digital" name="digital"> Digital copy (2.00)
数字拷贝(2.00)

以下是一个示例

希望对您有所帮助:

var项=0;//;
$('input[name=“delivery_method”]”)。在('change',function()上{
var合计=0;
if($(this).val()=='signed'){
$(“#交货价格”).html(£;2.00”);
$(“#投递方式”).html(“头等舱皇家邮政签名”);
总计=项目*4.99+2.00;
$(“#总成本”)。文本(总成本);
}else if($(this).val()==‘保证’){
$(“#交货价格”).html(£;6.22”);
$(“#投递方式”).html(“下午1点保证皇家邮政特快专递”);
总计=项目*4.99+6.22;
$(“#总成本”).html(总计);
}否则{
美元(“#交货价格”).html(£;0.00”);
$(“#投递方式”).html(“皇家邮政头等邮件”);
总计=项目*4.99;
$(“#总成本”).html(总计);
}
});

谢谢!这可以工作,但需要将该值添加到原来的4.99中,而不是将其重置。那怎么可能呢?
$('input[name="delivery_method"], input[name="digital"]').on('change', function () {
    var $del = $('input[name="delivery_method"]:checked');
    var $dig = $('input[name="digital"]');

    $("#delivery_price").html('&pound;' + $del.data('price'));
    $("#delivery_method").html($del.data('method'));

    var price = (parseFloat($del.data('price')) || 0) + ($dig.is(':checked') ? 2 : 0);
    $("#total_cost").html('&pound;' + price.toFixed(2));
})
 <input type="checkbox" ID='digital' value="digital" name="digital"> Digital copy (2.00)
var item = 0; //<?php echo ($item) ?>;

$('input[name="delivery_method"]').on('change', function(){
    var total = 0;
    if ($(this).val()=='signed') {
        $("#delivery_price").html("&pound;2.00");
        $("#delivery_method").html("Royal Mail Signed For First Class");
        total = item * 4.99 + 2.00;
        $("#total_cost").text(total);
    } else if($(this).val()=='guaranteed')  {
        $("#delivery_price").html("&pound;6.22");
        $("#delivery_method").html("Royal Mail Special Delivery Guaranteed by 1PM");
        total = item * 4.99 + 6.22;
        $("#total_cost").html(total);
    } else {
        $("#delivery_price").html("&pound;0.00");
        $("#delivery_method").html("Royal Mail First Class");
        total = item * 4.99;
        $("#total_cost").html(total);
    }
});