[英]fully change html element to another using javascript <button>NEXT</button> to <input type=“submit”>
I have a multi part form in which: when the user fills each page and clicks on a next button given below. 我有一个多部分的表单,其中:当用户填写每个页面并单击下面给出的下一个按钮时。 When on last page, the next button changes to a submit element but I am not able to submit data to database(MySQL XAMPP) I need input type="submit" instead of a button. 在最后一页上时,下一个按钮将更改为Submit元素,但是我无法将数据提交到数据库(MySQL XAMPP),我需要输入type =“ submit”而不是按钮。 I want to change all buttons to input type submit. 我想将所有按钮更改为输入类型提交。 My script is given below: 我的脚本如下:
HTML for button 按钮的HTML
<div style="float:right;">
<button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
<button type="button" name="submit1" id="nextBtn" onclick="nextPrev(1)">Next</button>
</div>
Script 脚本
function showTab1(n) {
// This function will display the specified tab of the form...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
//... and fix the Previous/Next buttons:
if (n == 0) {
document.getElementById("prevBtn").style.display = "none";
} else {
document.getElementById("prevBtn").style.display = "inline";
}
if (n == (x.length - 1)) {
document.getElementById("nextBtn").innerHTML = "Submit Form";
} else {
document.getElementById("nextBtn").innerHTML = "Next";
}
}
Add an input element under your button style="display: none;" 在您的按钮样式下添加输入元素style =“ display:none;” and give it an id="js-submit-form" 并给它一个id =“ js-submit-form”
<div style="float:right;">
<button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
<button type="button" name="submit1" id="nextBtn" onclick="nextPrev(1)">Next</button>
<input type="submit" id="js-submit-form" name="submit" value="Submit Form" style="display: none;">
</div>
Then I set display:none for #nextBtn and display:block for #js-submit-form 然后我为#nextBtn设置display:none和为#js-submit-form设置display:block
function showTab(n) {
// This function will display the specified tab of the form ...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
// ... and fix the Previous/Next buttons:
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("js-submit-form").style.display = "inline";
} else {
document.getElementById("nextBtn").style.display = "inline";
document.getElementById("js-submit-form").style.display = "none";
document.getElementById("nextBtn").innerHTML = "Next";
}
// ... and run a function that displays the correct step indicator:
fixStepIndicator(n)
} }
Then Style the input as you wish. 然后根据需要设置输入的样式。 Hope that helped ;-) 希望有帮助;-)
Just for information this multi part form was created by w3schools 仅供参考,此多部分表格由w3schools创建
First change the button
element to a input
type button element: 首先将button
元素更改为input
类型的button元素:
<div style="float:right;">
<input type="button" id="prevBtn" onclick="nextPrev(-1)" value="Previous" />
<input type="button" name="submit1" id="nextBtn" onclick="nextPrev(1)" value="Next" />
</div>
Then in Javascript you can change the type
of the submit button to submit
: 然后,在Javascript中,您可以将Submit按钮的type
更改为submit
:
function showTab1(n) {
// This function will display the specified tab of the form...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
//... and fix the Previous/Next buttons:
if (n == 0) {
document.getElementById("prevBtn").style.display = "none";
} else {
document.getElementById("prevBtn").style.display = "inline";
}
var nextBtn = document.getElementById("nextBtn");
if (n == (x.length - 1)) {
nextBtn.setAttribute('value', 'Submit Form');
nextBtn.setAttribute('type', 'submit');
nextBtn.removeAttribute('onclick');
} else {
nextBtn.setAttribute('value', 'Next');
nextBtn.setAttribute('type', 'button');
}
}
Be aware that you also have to remove or change the onclick
attribute of the submit button. 请注意,您还必须删除或更改提交按钮的onclick
属性。
I created what I think is a working snippet of your code… 我创建了我认为是您的代码的有效片段……
I think the best approach is to use another element for the submit. 我认为最好的方法是对提交使用另一个元素。
So, I added a hidden submit button, which shows when necessary. 因此,我添加了一个隐藏的提交按钮,该按钮在必要时显示。
Is that what you wanted to achieve? 这就是您想要实现的目标吗?
See my comments in the code: 在代码中查看我的评论:
// Added this vars here var tabs = document.getElementsByClassName("tab"); var prev = document.getElementById("prevBtn"); var next = document.getElementById("nextBtn"); var subm = document.getElementById("submBtn"); // New button var n = 0; function nextPrev(i) { // Renamed your function // Added: Hide all tabs for (var j = 0; j < tabs.length; j++) { tabs[n].style.display = "none"; } // This function will display the specified tab of the form... n = n + i; tabs[n].style.display = "block"; //... and fix the Previous/Next buttons: if (n == 0) { prev.style.display = "none"; } else { prev.style.display = "inline"; } if (n == (tabs.length - 1)) { next.style.display = "none"; subm.style.display = "inline"; } else { next.style.display = "inline"; subm.style.display = "none"; } } nextPrev(0); // Launch the function on load
.tab { display: none; } #submBtn { display: none; }
<form><!-- Added form --> <div style="float:right;"> <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button> <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button> <!-- Added this button --> <button type="submit" name="submit1" id="submBtn">Submit Form</button> </div> <!-- Added tabs --> <div class="tab">tab 0</div> <div class="tab">tab 1</div> <div class="tab">tab 2</div> <div class="tab">tab 3</div> </form>
⋅ ⋅ ⋅ ⋅⋅⋅
The disabled
property disabled
财产
Instead of showing/hide elements, you could use the disabled
property. 除了显示/隐藏元素,您还可以使用disabled
属性。
Here is the same code using it: 这是使用它的相同代码:
// Added this vars here var tabs = document.getElementsByClassName("tab"); var prev = document.getElementById("prevBtn"); var next = document.getElementById("nextBtn"); var subm = document.getElementById("submBtn"); // New button var n = 0; function nextPrev(i) { // Renamed your function // Added: Hide all tabs for (var j = 0; j < tabs.length; j++) { tabs[n].style.display = "none"; } // This function will display the specified tab of the form... n = n + i; tabs[n].style.display = "block"; //... and fix the Previous/Next buttons: if (n == 0) { prev.disabled = true; } else { prev.disabled = false; } if (n == (tabs.length - 1)) { next.disabled = true; subm.disabled = false; } else { next.disabled = false; subm.disabled = true; } } nextPrev(0); // Launch the function on load
.tab { display: none; }
<form><!-- Added form --> <div style="float:right;"> <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button> <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button> <!-- Added this button --> <button type="submit" name="submit1" id="submBtn">Submit Form</button> </div> <!-- Added tabs --> <div class="tab">tab 0</div> <div class="tab">tab 1</div> <div class="tab">tab 2</div> <div class="tab">tab 3</div> </form>
I hope it helps. 希望对您有所帮助。
On last form you add a type attribute to button element. 在最后一个表单上,将类型属性添加到按钮元素。 Like this 像这样
function showTab1(n) {
// This function will display the specified tab of the form...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
//... and fix the Previous/Next buttons:
if (n == 0) {
document.getElementById("prevBtn").style.display = "none";
} else {
document.getElementById("prevBtn").style.display = "inline";
}
if (n == (x.length - 1)) {
document.getElementById("nextBtn").setAttribute("type", "submit");
document.getElementById("nextBtn").innerHTML = "Submit Form";
} else {
document.getElementById("nextBtn").innerHTML = "Next";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.