3

我有一个选择下拉菜单来选择产品尺寸,它还会告诉您每种尺寸的价格。

我想显示总成本以及它将覆盖多少公顷。1 公斤 = 1 公顷

该值是尺寸的 ID,在添加到购物车时使用。

<select class="productChoice">
    <option value="">-- Please select --</option>
    <option value="2317474">1KG $180</option>
    <option value="2317475">5KG $100</option>
    <option value="2317476">10KG $200</option>
    <option value="2317477">50KG $900</option>
</select>

<input type="text" id="Units_5183442" class="productTextInput" value="0">

<label id="hectares_5183442"></label> Hectares
<label id="total_5183442"></label>

所以你选择你的产品尺寸,然后你想购买多少该尺寸。从那里我想在各自的标签中显示两个不同的值。

我正在努力分离选项文本来做我需要的事情。无法通过 NaN。

到目前为止,我得到的是:

$('#Units_5183442').keyup(multiply);
$('select.productChoice').change(multiply);
function multiply() {
    var quantity = parseFloat($('#Units_5183442').val());
    var dollars = cantfigureouthowtodothis
    var size = cantfigureouthowtodothis

    $('#total_5183442').text('$'+(quantity * dollars )); 
    $('#hectares_5183442').text((quantity * size)); 
}
4

5 回答 5

2

这将更新公顷和总成本标签:

$(function() {
    $('.productChoice').change(function() {

        if($('#Units_5183442').val() == 0)  // min 1
            $('#Units_5183442').val(1);

        var selectedText = $(this).find('option:selected').text(); 
        var match = selectedText.match(/\d{1,3}KG/g);
        if(match.length == 1)
        {
            var kg = parseInt(match[0].replace('KG',''));
            var costMatch = selectedText.match(/\$\d{1,3}/g);
            if(costMatch.length == 1)
            {
               var cost = parseInt(costMatch[0].replace('$',''));
               var quantity = parseInt($('#Units_5183442').val());  // presume you can only order integer quantities?

               $('#hectares_5183442').text(kg * quantity);
                $('#total_5183442').text('Total cost: $' + cost * quantity)
            }
        }
    });

    $('#Units_5183442').keyup(function() {
        $('.productChoice').trigger('change');
    });
});

JS 小提琴在这里:http: //jsfiddle.net/zud2Z/2/

于 2013-04-02T08:31:57.217 回答
0

这是使用提取值的一种方法split()

jsFiddle

var str = '1KG $180';
var kgs = str.split('KG')[0];
var dollars = str.split('$')[1];
alert('kgs: ' + kgs +
      '\ndollars: ' + dollars);
于 2013-04-02T08:23:12.310 回答
0

像这样的东西:

var calcResult = function() {

    var oSel = jQuery('.productChoice option:selected'),
        sText = oSel.text(),
        aData = sText.split(' '),
        mass = 0,
        price = 0,
        num = parseInt(jQuery('#Units_5183442').value());

    if( (aData.length > 1) && (aData[0] != '--') ) {
        mass = parseInt(aData[0]);
        price = parseInt(aData[1]);
    }

    jQuery('#total_5183442').text(num * price);
    jQuery('#hectares_5183442').text(num * mass);

};

jQuery('.productChoice').on('change', calcResult);
jQuery('#Units_5183442').on('change', calcResult);
于 2013-04-02T08:29:58.057 回答
0

或这个

var txt = '1KG $100';//assuming you've finally got and have validated this string
vals = txt.split('KG $');

var dollars = vals[1];
var size = vals[0]
于 2013-04-02T08:32:27.297 回答
0

您还可以为您的产品创建一个对象,然后将其用于您的计算。

var products = {
        '2317474':{
            qt:1,
            price:180
        }
    };

然后通过产品ID访问它

var prod = products[id];
于 2013-04-02T08:40:33.323 回答