【问题标题】:How send value from django html to django views如何将值从 django html 发送到 django 视图
【发布时间】:2021-12-24 22:36:17
【问题描述】:

我想制作一个商品部分,您可以在其中“+”和“-”增加或减少商品数量 然后,根据数量,“出售”它。

现在我们需要将值 {{ el.id }} 传递给 django 视图

我的代码: html:

    <form method="POST">
    {% csrf_token %}
    {% for el in form3 %}
    {% if el.count > 0 %}
           [ {{ el.count }}шт. ] <br>
           [ {{ el.title }} ]<br>
           <a id="minus{{ el.id }}" href="#"><b>[ - ]</b></a>
           <span id="value{{ el.id }}">0</span>
            <a id="plus{{ el.id }}" href="#"><b>[ + ]</b></a>
            <br>
Function where i + or - from count
<script>
$(function(){
    var valueElement = $('#value{{ el.id }}');
    function incrementValue(e){
        valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment, 0));
        return false;

    }
    $('#plus{{ el.id }}').bind('click', {increment: 1}, incrementValue);
    $('#minus{{ el.id }}').bind('click', {increment: -1}, incrementValue);
});
</script>
            {% endif %}
            {% endfor %}
       </form>

我如何使用“span id=value{{ el.id }}”获取值

【问题讨论】:

  • 为什么,你可以向服务器发送一个 HTTP 请求并减少或增加该值。您可以填写href

标签: javascript python html jquery django


【解决方案1】:

解决了

    {% for el in form3 %}
    {% if el.count > 0 %}
           [ {{ el.count }}шт. ] <br>
           [ {{ el.title }} ]<br>
           <a id="minus{{ el.id }}" href="#"><b>[ - ]</b></a>
           <span id="span-value{{el.id}}">0</span>
           <a id="plus{{ el.id }}" href="#"><b>[ + ]</b></a>
        <br>
                <input type="hidden" name=name-{{el.id}} id="input-{{el.id}}" value="0">
        <script>
            $(function(){
            var valueElement = $('#span-value{{el.id}}');
            function incrementValue(e){
            valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment, 0));
    var value = $("#span-value{{el.id}}").text();
    var lower=$("#input-{{el.id}}").val(value);
            return false;
            }
                $('#plus{{ el.id }}').bind('click', {increment: 1}, incrementValue);
                $('#minus{{ el.id }}').bind('click', {increment: -1}, incrementValue);
            });
        </script>
    {% endif %}
    {% endfor %}

在输入中使用它来复制\复制跨度值 在此之后,您可以使用 request.post.dict 或 request.post.items() for key:values

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2013-01-21
    相关资源
    最近更新 更多