简体   繁体   English

jQuery在下拉选择中将两个值相乘

[英]jQuery Multiply two values on drop down select

Hi I have a bit of code which once you select drop value the price field updates 嗨,我有一些代码,一旦您选择了下降值,价格字段就会更新

3 or more selected from drop down price = £10.99 each 2 price = £11.99 each 1 price = £12.99 each 从下拉价格中选择3个或更多=每只£10.99 2个价格=每只£11.99 1个价格=每只£12.99

jQuery(document).ready( function(){

        var map = [ '12.99', '11.99', '10.99' ];

        jQuery('#payslips-required').change(function(){

            var o = parseInt($(this).val()) < 3 ? jQuery(this).val()-1 : 2;

            jQuery('#price').val(map[o]).addClass('hidden');

        });

    });

Currently the above works but when I add a total field right next to it and amend the jQuery to work out the total, price x number selected 目前上述方法有效,但是当我在其旁边添加一个total字段并修改jQuery以计算出total时,则选择价格x数

Here is the example in action http://jsfiddle.net/U92fq/1/ which works 这是实际的示例http://jsfiddle.net/U92fq/1/

but when I place the added code which is 但是当我放置添加的代码时

var price = $('#price').val()   
            var quantity = $('#payslips-required').val()    
            var total = price * quantity;
            jQuery('#total').val(total).addClass('hidden');

Into into the original code does not work when it clearly does on the jsfiddle link provided both the price and total do not display. 如果价格和总计均未显示,则在jsfiddle链接上清楚地显示为原始代码无效。

Just can't see why it does not work. 只是看不到为什么它不起作用。

Summary: Need to get the total to work. 简介:需要使全部工作。

Note: using this on Wordpress with Contact form 7 plugin can be seen here http://payslips4u.co.uk/order-monthly-payslips/ 注意:在Wordpress上使用带有联系表7插件的插件可以在此处查看http://payslips4u.co.uk/order-monthly-payslips/

Use semicolon end of each line 使用每行的分号结尾

 jQuery('#price').val(map[o]).addClass('hidden');
            var price = $('#price').val();  
            var quantity = $('#payslips-required').val();   
            var total = price * quantity;
            jQuery('#total').val(total).addClass('hidden');

Either use semicolon (;) at the end of each var or put one var with comma seperation 在每个变量的末尾使用分号(;)或使用逗号分隔放置一个变量

var price = $('#price').val(),  
    quantity = $('#payslips-required').val(),   
    total = price * quantity;

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

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