[英]Multi-step form, next step button won't work
onclick event for button:
按钮的onclick事件:
<button onclick="processPhase1()">Continue</button>
Function:
功能:
function processPhase1() {
("phase1").style.display = "none";
("phase2").style.display = "block";
}
Example form (to display my structure):
表格示例(显示我的结构):
<form>
<div id="phase1">
<!-- Some Input field -->
<!-- Some Input field -->
<button onclick="processPhase1()">Next Step</button>
</div>
<div id="phase2">
<!-- Some Input field -->
<!-- Some Input field -->
</div>
</form>
Of course, the 2nd phase isn't displayed when I'm trying to go to next step, since I've the suitable css for it. 当然,当我尝试进行下一步时,不会显示第二阶段,因为我有合适的CSS。 The problem is, it wont go to next step.
问题是,它不会进行下一步。
If you want to take a look in my code just let me know. 如果您想看一下我的代码,请告诉我。
EDIT: Sorry not to mention it earlier. 编辑:对不起,更不用提了。 I'm using
我正在使用
function _(x) {
return document.getElementById(x);
}
and then putting _ infront of the ("phase1"),("phase2") but it still won't work 然后将_放在(“ phase1”),(“ phase2”)的前面,但仍然无法正常工作
If the button
is inside a form (unknown based on your code), and the type
of the button is not specified (as above), it will default to submit
and submit that form. 如果
button
在表单内(根据您的代码未知),并且未指定按钮的type
(如上所述),则默认将submit
并提交该表单。 It's possible your page is just reloading on the form submission. 您的页面可能只是在表单提交时重新加载。
The simplest fix is to return false;
最简单的解决方法是
return false;
after processing the click: 处理完点击后:
<button onclick="processPhase1(); return false;">Next Step</button>
Your use of "_" as a stand-in for getElementById seems to needlessly risk conflict (or at least confusion) with the underscore library, but it does work: 您将“ _”用作getElementById的替代品似乎不必要地冒险与下划线库发生冲突(或至少造成混淆),但它确实起作用:
function _(x) { return document.getElementById(x); } function processPhase1() { _("phase1").style.display = "none"; _("phase2").style.display = "block"; }
<form> <div id="phase1"> This is phase 1 <button onclick="processPhase1()">Next Step</button> </div> <div id="phase2" style="display:none"> This is phase 2 </div> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.