从js获取值并将其作为参数传递

时间:2018-10-26 16:49:46

标签: javascript python jquery

我在HTML文件中有一个表格:

{% for dimension in dimension_list %}
     <tr>
      <td>{{dimension.title}}</td>
      <td>{{dimension.description}}</td>
      <div class="button-group">          
        <td><button type="button" class="btn-click-del" data- 
        id="{{dimension.pk}}" id="btn-confirm" ></button></td>
      </div>
     </tr>  
{% endfor %}  

因此,我接收到值data-id(按钮)并将其传递给模型:

 $('.btn-click-del').on('click', function () {      
    var id = ($(this).data('id'));
    $("#modal-btn-yes").val(id);
});

在模式中,我需要在“是”按钮中将此id作为参数传递给函数Dimension_remove

div class="modal-dialog modal-bg modal-dialog-centered">
    <button onclick="location.href='{% url 'dimension_remove' pk=dimension.id %}';" type="button" class="btn btn-default" id="modal-btn-yes">Yes</button>
    <button type="button" class="btn btn-primary" id="modal-btn-no">No</button>    
</div>

1 个答案:

答案 0 :(得分:1)

我建议将click事件附加到您的JS部分中,并将id附加到data-idmodal-btn-yes上,然后简单地获取它并构建您的URL。

data-id附加到modal-btn-yes按钮上:

$('.btn-click-del').on('click', function() {
  $("#modal-btn-yes").data('id', $(this).data('id'));
});

获取data-id并构建URL:

$('body').on('click', '#modal-btn-yes', function() {
  //Here you could get the clicked button "id" like
  var id = $(this).data('id');

  //location.href= Your link here
});