我一直在尝试在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的代码,并获得相似的结果。
该如何解决?
答案 0 :(得分:0)
对于可能会遇到类似问题的人,我将其修正为:
我没有将提交按钮单独保留,而是将其固定在最后一个“ form-group-tab” div
UIColor.placeholderText
然后我编辑了javascript逻辑,以停止在最终标签中显示“下一步”按钮。
<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>
希望有帮助