简体   繁体   English

如何通过 JavaScript 在 request.POST 中发送当前表单选项卡

[英]How to send the current form tab in a request.POST via JavaScript

I have a form, in the form I have a series of objects, for each object there is a tab.我有一个表格,表格中有一系列对象,每个 object 都有一个选项卡。 For example:例如:

object 1: tab1 object 2: tab2 object 3: tab3 object 1: tab1 object 2: tab2 object 3: tab3

Code example:代码示例:

<ul class="nav nav-pills" id="evidence-formset-tab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" id="evidence-form-1-tab" data-toggle="pill" href="#evidence-form-1" role="tab" aria-controls="evidence-form-1" aria-selected="true">1</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="evidence-form-2-tab" data-toggle="pill" href="#evidence-form-2" role="tab" aria-controls="evidence-form-2" aria-selected="false">2</a>
  </li>
</ul>

I would like to know the value of the selected tab at the time of submitting the form.我想知道提交表单时所选选项卡的值。

JavaScript part: JavaScript 部分:

function validateRegister() {
    event.preventDefault();
    $('#evidence-formset').attr('action', "{% url 'interpretation' %}").submit();
}

Is there a way to inject the value of the current tab and then process it in the (request.POST) in the backend?有没有办法注入当前选项卡的值,然后在后端的(request.POST)中处理它?

You can use $(".nav-item.active a").text() to get value of active tab and then put this value inside input tag and append it to your form so that this will also get submitted.您可以使用$(".nav-item.active a").text()来获取活动选项卡的值,然后将此值放入输入标签中,并将 append 放入您的表单中,这样它也将被提交。

Demo Code :演示代码

 function validateRegister() { event.preventDefault(); console.log($(".nav-item.active a").text()) //get class which is active text() and append inside form.. $('#evidence-formset').append("<input type='text' name='obj' value='" + $(".nav-item.active a").text() + "'>") $('#evidence-formset').attr('action', "{% url 'interpretation' %}").submit(); }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <ul class="nav nav-pills" id="evidence-formset-tab" role="tablist"> <li class="nav-item active"> <a class="nav-link active" id="evidence-form-1-tab" data-toggle="pill" href="#evidence-form-1" role="tab" aria-controls="evidence-form-1" aria-selected="true">1</a> </li> <li class="nav-item"> <a class="nav-link" id="evidence-form-2-tab" data-toggle="pill" href="#evidence-form-2" role="tab" aria-controls="evidence-form-2" aria-selected="false">2</a> </li> </ul> <div class="tab-content"> <div id="evidence-form-1" class="tab-pane fade in active"> <h3>HOME</h3> </div> <div id="evidence-form-2" class="tab-pane fade"> <h3>Menu 1</h3> </div> </div> <form id="evidence-formset"> <button type="submit" onclick="validateRegister()">Click me</button> </form>

I assume you are using Bootstrap navs .我假设您正在使用Bootstrap navs If this is correct, you should mention it first (and tag your qeustion as such).如果这是正确的,您应该首先提及它(并标记您的问题)。

You can add a <input type="hidden" name="activetab" id="activetab"> tag somewhere inside your <form></form> .您可以在<form></form>内的某处添加<input type="hidden" name="activetab" id="activetab">标记。 Then add an event listener that updates the value of this hidden input whenever the user changes the tab:然后添加一个事件侦听器,每当用户更改选项卡时更新此隐藏输入的值:

$('a[data-toggle="pill"]').on('shown.bs.tab', (e) => {
  $('#activetab').val(e.target.href.replace(/^#/, ''));
})

For example, if the user clicks on Tab 1, this will set the value of the input to "evidence-form-1" .例如,如果用户点击 Tab 1,这会将输入的值设置为"evidence-form-1" Then, when the user submits the form, the browser will send the value of the hidden input to the server as activetab=evidence-form-1 , which can be read from Django.然后,当用户提交表单时,浏览器会将隐藏输入的值作为activetab=evidence-form-1发送给服务器,可以从 Django 读取。

Note that the input won't have a value until the user switches tabs at least once.请注意,在用户至少切换一次选项卡之前,输入不会有值。 This may become a problem if the user does not click on any tabs before submitting the form.如果用户在提交表单之前没有单击任何选项卡,这可能会成为一个问题。 I'll leave it to you to figure out how to handle this scenario.我将留给您弄清楚如何处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM