【发布时间】:2019-10-19 16:30:41
【问题描述】:
我一直在尝试在我的 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";
}
还有我的观点.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 的代码,结果相似。
我应该做些什么来解决这个问题?
【问题讨论】:
标签: javascript flask flask-wtforms