【问题标题】:how can I pass the id of the item selected from the dropdown in django form action url?如何传递从 django 表单操作 url 的下拉列表中选择的项目的 ID?
【发布时间】:2021-09-05 23:52:39
【问题描述】:

问题 在此我想将交易项目的 id 从下拉列表中传递到表单操作 URL cart_add。 从下拉列表中选择的值中,我获取了 id,但现在我不知道如何将该 id 传递给脚本,然后以添加到购物车的形式传递。如果有人知道请帮忙。

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script>
   $(document).on("change", '.Price', function (event) {
           event.preventDefault();
           $('#id_price').text($(this).children(":selected").attr("price"));
           $('#id_sale_price').text($(this).children(":selected").attr("sale_price"));
           $('#transactionIDValue').val($(this).children(":selected").attr("transID"));
    });
</script>



    <select class="Price" style="width: 250px;">
      <option value="none" selected disabled hidden>
        Select an Option
    </option>
    {% for item in transaction %}
    
      <option transID={{item.id}} price={{item.Price}} sale_price={{item.get_sale}} >{{item.AUID.unit}} - {{item.Description}}</option>

    {% endfor %}
  </select>

    <form id='transactionIDValue' action="{% url 'cart:cart_add' %}" class="d-inline" method="post">
      {{cart_product_form}}
      {% csrf_token %}
      <input type="submit" class="btn btn-primary shadow px-5 py-2" value="Add To Cart">
      <!-- <button type="submit" class="btn btn-primary shadow px-5 py-2">Add to Cart</button> -->
    </form>
 

【问题讨论】:

  • 您能否突出显示您希望人们查看的代码,并提供有关您尝试过的内容以及您在试用过程中发现的问题的更多详细信息。
  • 更新它检查你是否可以'

标签: javascript html jquery django django-templates


【解决方案1】:

只需在你的脚本标签中添加操作 URL,就像这样检查工作 sn-p

$(document).on("change", '.Price', function (event) {
           event.preventDefault();
           let id = $('#customselect').find(":selected").attr('price');
           console.log('Id : ',id)
         let Url = `/page/${id}`  
         $('#transactionIDValue').attr('action', Url);
         let formAction = $('#transactionIDValue').attr('action')
         let formAction2 = $('#transactionIDValue').prop('action')
         console.log('form action relative  url : ',formAction)
         console.log('form action full url : ',formAction2)
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select class="Price" id="customselect">
<option value="">----</option>
<option value="1" price="1">1</option>
<option value="2" price="2">2</option>
<option value="3" price="3">3</option>
</select>

<form id="transactionIDValue">
</form>

【讨论】:

  • 它给出了什么输出。
  • 它没有将 id 传递给 url。
  • 你好@MuhammadUsamaNaveed 检查我已经编辑了我的答案。
  • 这里运行良好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
相关资源
最近更新 更多