簡體   English   中英

在Javascript中調用Flask-Wtform提交按鈕

[英]Calling a Flask-Wtform submit button in Javascript

我一直在嘗試在我的Flask應用中設置一個多步驟表單。

我在w3schools上發現了本教程,該教程非常有幫助: https : //www.w3schools.com/howto/howto_js_form_steps.asp

經過一些代碼調整,該表單可以正確呈現,如本教程中所示。

我的代碼如下所示:

HTML:


<form method="POST" name="register_workplace_form", name="form" class="regForm" id="regForm" action="/">

{{ form.hidden_tag() }}

<div class="form-group"> 
   <h3 class="form_labels">{{ _('Register Workplace') }} </h3> 
</div>                

<div class="form-group tab">
     {{render_field_with_errors(form.name, class='form-control',placeholder='Name of Workplace') }}
</div>
.
.
.Several other 'form-group tab' divs
.
.
<div style="overflow:auto;">
    <div style="float:right;">

      <button type="button" id="prevBtn" onclick="nextPrev(-1)" class="btn btn-primary">Previous</button>
      <button type="button" id="nextBtn" onclick="nextPrev(1)" class="btn btn-primary">Next</button>


      <div class="form-group">
        <div class="col-md-12 " id="final">
          {{ form.submit(class='btn btn-primary pull-left final') }}
        </div>
      </div>

     </div>
</div>

JAVASCRIPT:

var currentTab = 0;
showTab(currentTab);

function showTab(n) {
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";
  if (n == 0) {
    document.getElementById("prevBtn").style.display = "none";
  } else {
    document.getElementById("prevBtn").style.display = "inline";
  }

  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").style.display = "none";
    document.getElementById("final").style.display = "inline";
       <!--document.getElementById("nextBtn").innerHTML = "Submit";-->
  } else {
    document.getElementById("final").style.display = "none";
    document.getElementById("nextBtn").style.display = "inline";
  }

  // ... and run a function that displays the correct step indicator:
  fixStepIndicator(n)
}

function nextPrev(n) {
  // This function will figure out which tab to display
  var x = document.getElementsByClassName("tab");

  // Exit the function if any field in the current tab is invalid:
  if (n == 1 && !validateForm()) return false;

  // Hide the current tab:
  x[currentTab].style.display = "none";

  // Increase or decrease the current tab by 1:
  currentTab = currentTab + n;

  // if you have reached the end of the form... :
  if (currentTab >= x.length) {

  //...the form gets submitted:

    document.getElementById("regForm").submit();
    return false;
  }
  // Otherwise, display the correct tab:
  showTab(currentTab);
}

function validateForm() {
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  for (i = 0; i < y.length; i++) {
    if (y[i].value == "") {
      y[i].className += " invalid";
      valid = false;
    }
  }
  // If the valid status is true, mark the step as finished and valid:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid;
}

function fixStepIndicator(n) {

  // This function removes the "active" class of all steps...
  var i, x = document.getElementsByClassName("step");
  for (i = 0; i < x.length; i++) {
    x[i].className = x[i].className.replace(" active", "");
  }

  //... and adds the "active" class to the current step:
  x[n].className += " active";
}

還有我的views.py

@public.route('/', methods = ['GET','POST'])
def index():
    form = workplaceForm()

    if request.method == 'POST' and form.validate():
        workplace = Workplace( \
                      name=form.name.data,created_at=datetime.now(),status='active')
        db.session.add(workplace)
        db.session.commit
        flash('You have successfully registered your workplace, pending registration fee payment.')

    return render_template('index.html', form = form)

但是,由於某些原因,最后一個選項卡上的“提交”按鈕是無效的,因此單擊后它不會執行任何操作。

我嘗試將其更改為輸入標簽內的簡單按鈕,並將其還原為原始w3school的代碼,並獲得相似的結果。

我應該采取什么其他措施來解決此問題?

對於可能會遇到類似問題的人,我將其修正為:

我沒有將提交按鈕單獨保留,而是將其固定在最后一個“ form-group-tab” div中


<div class="form-group tab">
 {{render_field_with_errors(form.premAddr, class='form-control', placeholder='Address of Premise') }}
 {{ form.submit(class='btn btn-primary pull-left final') }}
</div>

然后,我編輯了javascript邏輯,以停止在最終標簽中顯示“下一步”按鈕:


  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").style.display = "none";

  } else {
    document.getElementById("nextBtn").style.display = "inline";
    document.getElementById("nextBtn").innerHTML = "Next";
  }

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM