英文:
submit master form with only active detail tab form
问题
这是您的表单代码,包括一个主表单和两个不同选项卡上的两个详细信息部分(提交按钮仅位于选项卡之外的主表单上)。我正在尝试仅使用单击提交主表单中的活动选项卡(第一个选项卡或第二个选项卡)。目前,当我尝试仅使用活动选项卡提交数据时,只有在非活动选项卡的强制字段填写完毕后,才会提交数据。
以下是代码部分:
<form method="POST" action="abc.php">
<input type="text" name="mrno" id="mrno"><br>
<input type="text" name="name1" id="name1"><br>
<div class="tab">
<button class="tablinks" type="button" onclick="openCity(event, 'London')">Value Range Result</button>
<button class="tablinks" type="button" onclick="openCity(event, 'Paris')">Fixed Test Result</button>
</div>
<div id="London" class="tabcontent">
<input type="text" name="maleage" id="maleage"><br>
<input type="text" name="maleedu" id="maleedu"><br>
</div>
<div id="Paris" class="tabcontent">
<input type="text" name="femage" id="femage"><br>
<input type="text" name="femedu" id="femedu"><br>
</div>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
JavaScript 代码:
<script>
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
请注意,这只是您的代码的翻译部分。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。
英文:
I have my form which consist of one master form and 2 details parts which on two different tab (submit button is on only main form outside tabs.). I am trying to submit master form with only active tab(either first tab or 2nd tab) form with single click. Currently when i submit try to submit data with active tab only, then its not submitted until mandotry fields of inactive tab are fill
here is the code :
`<form method="POST" action="abc.php">
<input type="text" name= "mrno" id= "mrno" > <br>
<input type="text" name= "name1" id= "name1" > <br>
<div class="tab">
<button class="tablinks" type="button" onclick="openCity(event, 'London')">Value Range Result</button>
<button class="tablinks" type="button"onclick="openCity(event, 'Paris')">Fixed Test Result</button>
</div>
<div id="London" class="tabcontent">
<input type="text" name= "maleage" id= "maleage" > <br>
<input type="text" name= "maleedu" id= "maleedu" > <br>
</div>
<div id="Paris" class="tabcontent">
<input type="text" name= "femage" id= "femage" > <br>
<input type="text" name= "femedu" id= "femedu" > <br>
</div>`
Java script code
`<script>
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
<input type="submit" name="submit" id="submit" value="Submit">
</form> `
答案1
得分: 1
您当前的设置会提交表单以及子部分中所有命名输入。您想要避免提交位于非活动选项卡内的字段。一个可能的解决方案是通过在更改选项卡时删除旧项目并添加新项目来更改表单的内部内容,但基于多种原因,我不建议采用这种方法,例如:
- 您可能已为这些字段实施了一些JavaScript事件,如果更改结构,这些事件将丢失,您将需要重新创建它们。
- 您已经实现了激活和停用这些选项卡的逻辑,我们希望避免强制您进行根本性的更改。
因此,与其更改结构,让我们更改属性。
每当切换选项卡焦点时,我们希望删除字段内的name
属性,并将适当的name
属性添加到活动选项卡内的输入字段。
请注意,我已默认设置了第一个选项卡,并且不是使非活动选项卡消失,为了测试的目的,我已对命名输入应用了颜色,换句话说,您随时可以知道如果提交表单,将发送什么到服务器。另请注意,我对您的代码进行了重构,以使其更易于阅读。您可以轻松将此从命名输入的颜色更改为隐藏未命名的属性:
解释逻辑:我们循环遍历具有class
为tabcontent
的元素,并针对每个元素循环遍历具有data-name
属性的其输入字段(请注意,我已将此属性添加到结构中,具有与name
将具有的相同值,因此每当我们创建/删除name
属性时,其所需的值仍然作为data-name
属性的值可用),如果我们在活动选项卡内,则正确设置其name
属性。否则,如果我们在非活动选项卡内,则删除其name
属性。
英文:
Your current setup submits your form along with all the named inputs of your subsections. You want to avoid submitting the fields that are inside inactive tabs. A possible solution is to change the inner content of the form, by removing old items and adding new items whenever you change the tab, but I do not recommend that approach for your case for several reasons, like:
- you may have some Javascript events for these fields which will be lost if you change the structure and you would need to recreate them
- you already implemented a logic for deactivating and activating these tabs and we want to avoid to compel you into radical changes
So, instead of changing the structure, let's change the attributes.
We whenever we switch focus on a tab, we want to remove the name
property of the fields inside and we want to add a proper name property to the inputs inside the active tab.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
//tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
evt.currentTarget.classList.add("active");
//document.getElementById(cityName).classList.add("active");
for (let tabcontent of document.getElementsByClassName("tabcontent")) {
for (let input of tabcontent.querySelectorAll("[data-name]")) {
if (tabcontent.id === cityName) {
input.setAttribute("name", input.getAttribute("data-name"));
} else {
input.removeAttribute("name");
}
}
}
}
<!-- language: lang-css -->
.active {
color: green;
}
form [name] {
background-color: lightgreen;
}
<!-- language: lang-html -->
<form method="POST" action="abc.php">
<input type="text" name= "mrno" id= "mrno" > <br>
<input type="text" name= "name1" id= "name1" > <br>
<div class="tab">
<button class="tablinks active" type="button" onclick="openCity(event, 'London')">Value Range Result</button>
<button class="tablinks" type="button"onclick="openCity(event, 'Paris')">Fixed Test Result</button>
</div>
<div id="London" class="tabcontent">
<input type="text" name="maleage" data-name= "maleage" id= "maleage" > <br>
<input type="text" name="maleedu" data-name= "maleedu" id= "maleedu" > <br>
</div>
<div id="Paris" class="tabcontent">
<input type="text" data-name= "femage" id= "femage" > <br>
<input type="text" data-name= "femedu" id= "femedu" > <br>
</div>
</form>
<!-- end snippet -->
Note that I defaulted your first tab and, instead of making the inactive tab disappear, for the purpose of the test I have applied coloring for the named inputs, in other words, at any moment you know what would be sent to the server if the form is to be submitted. Note further that I refactored your code to make it more readable. It's easy to change this from coloring named inputs as green to hide unnamed attributes:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
//tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
evt.currentTarget.classList.add("active");
//document.getElementById(cityName).classList.add("active");
for (let tabcontent of document.getElementsByClassName("tabcontent")) {
for (let input of tabcontent.querySelectorAll("[data-name]")) {
if (tabcontent.id === cityName) {
input.setAttribute("name", input.getAttribute("data-name"));
} else {
input.removeAttribute("name");
}
}
}
}
<!-- language: lang-css -->
.active {
color: green;
}
form input:not([name]) {
display: none;
}
<!-- language: lang-html -->
<form method="POST" action="abc.php">
<input type="text" name= "mrno" id= "mrno" > <br>
<input type="text" name= "name1" id= "name1" > <br>
<div class="tab">
<button class="tablinks active" type="button" onclick="openCity(event, 'London')">Value Range Result</button>
<button class="tablinks" type="button"onclick="openCity(event, 'Paris')">Fixed Test Result</button>
</div>
<div id="London" class="tabcontent">
<input type="text" name="maleage" data-name= "maleage" id= "maleage" value="a" > <br>
<input type="text" name="maleedu" data-name= "maleedu" id= "maleedu" value="b" > <br>
</div>
<div id="Paris" class="tabcontent">
<input type="text" data-name= "femage" id= "femage" value="c" > <br>
<input type="text" data-name= "femedu" id= "femedu" value="d" > <br>
</div>
</form>
<!-- end snippet -->
Explanation of the logic: We loop our elements having the class
of tabcontent
and for each of them we loop their inputs having a data-name
attribute (notice that I have added this attribute to the structure, having the same value as name
would have, so whenever we create/remove the name
attribute, its desired value is still available as the value of the data-name
attribute) and, if we are inside the active tab, then we properly set its name
. Otherwise, if we are at an inactive tab, then we remove its name
attribute.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论