计算Django Inline内的价格

时间:2015-07-01 21:54:58

标签: javascript jquery python django

我正在寻找一些方法来使用一些javascript从Django(动态)内联计算价格。

我有这个场景(在Django Admin中): enter image description here

我正试图做那样的事情:



{% block extrajs %}
    <script type="text/javascript">
        var subtotal = 0.00;
        $('.field-product_code select').change(function(){
            $('.related-widget-wrapper select').each(function(){
                product = $(this + 'option:selected').text();
                select_val = $(this).val();
                var regex = /[+-]?\d+(\.\d+)?/g;

                var str = product;
                var floats = str.match(regex).map(function(v) { return parseFloat(v); });
                var price = floats[0] * 1;
                console.log($(this).val());
            });
//            row_id = $(this).closest('tr').attr('id');
//            console.log($(row_id).find('.price_product'));
{#            if(row_id + ' .price_product'){#}
{#                console.log('existe ja');#}
{##}
{#            }else {#}
{#                //$(this).append("<div class='price'>aa"+ product +"</div>");#}
{#                var regex = /[+-]?\d+(\.\d+)?/g;#}
{##}
{#                var str = product;#}
{#                var floats = str.match(regex).map(function(v) { return parseFloat(v); });#}
{#                var price = floats[0] * 1;#}
{#                console.log(price);#}
{#            }#}


{#            subtotal += price;#}
{#            console.log(subtotal);#}
{#            $('#total').html(subtotal);#}
        })
    </script>
{% endblock %}
&#13;
<!-- The inline HTML extracted with inspector -->
<table class="table table-bordered table-condensed table-striped">
<thead><tr>
    <th colspan="1" class="required">Código do Produto
    </th>
    <th>Quantidade
    </th>
    <th>Apagar?</th>
    </tr></thead>

<tbody>
    <tr class="form-row  dynamic-movementcart_set row1" id="movementcart_set-0">
        <td class="field-product_code">
            <input id="id_movementcart_set-0-id" name="movementcart_set-0-id" type="hidden">
            <input id="id_movementcart_set-0-movement" name="movementcart_set-0-movement" type="hidden">
            <div class="related-widget-wrapper">
                <select id="id_movementcart_set-0-product_code" name="movementcart_set-0-product_code">
                    <option value="" selected="selected">---------</option>
                    <option value="8888">Água Mineral - R$ 0.69</option>
                    <option value="9999">Coca-Cola Lata - R$ 1.99</option>
                    <option value="7891027114626">Vodka - R$ 20.0</option>
                </select>
                <a class="related-widget-wrapper-link change-related" id="change_id_movementcart_set-0-product_code" data-href-template="/painel/engine/product/__fk__/?_to_field=product_code&amp;_popup=1" title="Alterar Produto selecionado" href="/painel/engine/product/8888/?_to_field=product_code&amp;_popup=1">
                    <img src="/static/admin/img/icon_changelink.gif" width="10" height="10" alt="Modificar">
                </a>
                <a class="related-widget-wrapper-link add-related" id="add_id_movementcart_set-0-product_code" href="/painel/engine/product/add/?_to_field=product_code&amp;_popup=1" title="Adicionar outro Produto">
                    <img src="/static/admin/img/icon_addlink.gif" width="10" height="10" alt="Adicionar">
                </a>
            </div>
        </td>
        <td class="field-qtde">
            <p>1</p>
        </td>
        <td class="delete"></td>
    </tr>
    <tr class="form-row dynamic-movementcart_set row2" id="movementcart_set-1">

        <td class="field-product_code">


            <input id="id_movementcart_set-1-id" name="movementcart_set-1-id" type="hidden">
            <input id="id_movementcart_set-1-movement" name="movementcart_set-1-movement" type="hidden">







            <div class="related-widget-wrapper">
                <select id="id_movementcart_set-1-product_code" name="movementcart_set-1-product_code">
                    <option value="" selected="selected">---------</option>
                    <option value="8888">Água Mineral - R$ 0.69</option>
                    <option value="9999">Coca-Cola Lata - R$ 1.99</option>
                    <option value="7891027114626">Vodka - R$ 20.0</option>
                </select>


                <a class="related-widget-wrapper-link change-related" id="change_id_movementcart_set-1-product_code" data-href-template="/painel/engine/product/__fk__/?_to_field=product_code&amp;_popup=1" title="Alterar Produto selecionado" href="/painel/engine/product/9999/?_to_field=product_code&amp;_popup=1">
                    <img src="/static/admin/img/icon_changelink.gif" width="10" height="10" alt="Modificar">
                </a>


                <a class="related-widget-wrapper-link add-related" id="add_id_movementcart_set-1-product_code" href="/painel/engine/product/add/?_to_field=product_code&amp;_popup=1" title="Adicionar outro Produto">
                    <img src="/static/admin/img/icon_addlink.gif" width="10" height="10" alt="Adicionar">
                </a>



            </div>



        </td>



        <td class="field-qtde">





            <p>1</p>


        </td>

        <td class="delete"><div><a class="inline-deletelink" href="javascript:void(0)">Remover</a></div></td>
    </tr><tr class="form-row  empty-form row1" id="movementcart_set-empty">
    <td class="field-product_code">

        <input id="id_movementcart_set-__prefix__-id" name="movementcart_set-__prefix__-id" type="hidden">
        <input id="id_movementcart_set-__prefix__-movement" name="movementcart_set-__prefix__-movement" type="hidden">

        <div class="related-widget-wrapper">
            <select id="id_movementcart_set-__prefix__-product_code" name="movementcart_set-__prefix__-product_code">
                <option value="" selected="selected">---------</option>
                <option value="8888">Água Mineral - R$ 0.69</option>
                <option value="9999">Coca-Cola Lata - R$ 1.99</option>
                <option value="7891027114626">Vodka - R$ 20.0</option>
            </select>


            <a class="related-widget-wrapper-link change-related" id="change_id_movementcart_set-__prefix__-product_code" data-href-template="/painel/engine/product/__fk__/?_to_field=product_code&amp;_popup=1" title="Alterar Produto selecionado">
                <img src="/static/admin/img/icon_changelink.gif" width="10" height="10" alt="Modificar">
            </a>


            <a class="related-widget-wrapper-link add-related" id="add_id_movementcart_set-__prefix__-product_code" href="/painel/engine/product/add/?_to_field=product_code&amp;_popup=1" title="Adicionar outro Produto">
                <img src="/static/admin/img/icon_addlink.gif" width="10" height="10" alt="Adicionar">
            </a>

        </div>

    </td>

    <td class="field-qtde">

        <p>1</p>

    </td>

    <td class="delete"></td>
    </tr>

    <tr class="add-row"><td colspan="3"><a href="javascript:void(0)">Adicionar outro(a) Item</a></td></tr></tbody>
</table>
&#13;
&#13;
&#13;

也许我必须计算&#39;所有#id_movementcart_set-0-id#id_movementcart_set-1-id#id_movementcart_set-2-idsubsequents,但我该怎么做?

0 个答案:

没有答案