提交主表单,仅包含活动详细信息选项卡的表单。

huangapple go评论77阅读模式
英文:

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 :

      `&lt;form method=&quot;POST&quot; action=&quot;abc.php&quot;&gt;
       &lt;input type=&quot;text&quot; name= &quot;mrno&quot;  id= &quot;mrno&quot; &gt; &lt;br&gt;
       &lt;input type=&quot;text&quot; name= &quot;name1&quot;  id= &quot;name1&quot; &gt; &lt;br&gt;
        &lt;div class=&quot;tab&quot;&gt;
        &lt;button class=&quot;tablinks&quot; type=&quot;button&quot; onclick=&quot;openCity(event, &#39;London&#39;)&quot;&gt;Value Range          Result&lt;/button&gt;
              &lt;button class=&quot;tablinks&quot; type=&quot;button&quot;onclick=&quot;openCity(event, &#39;Paris&#39;)&quot;&gt;Fixed Test       Result&lt;/button&gt;
        &lt;/div&gt;
         &lt;div id=&quot;London&quot; class=&quot;tabcontent&quot;&gt;
         &lt;input type=&quot;text&quot; name= &quot;maleage&quot;  id= &quot;maleage&quot; &gt; &lt;br&gt;
          &lt;input type=&quot;text&quot; name= &quot;maleedu&quot;  id= &quot;maleedu&quot; &gt; &lt;br&gt;
            &lt;/div&gt;

          &lt;div id=&quot;Paris&quot; class=&quot;tabcontent&quot;&gt;
         &lt;input type=&quot;text&quot; name= &quot;femage&quot;  id= &quot;femage&quot; &gt; &lt;br&gt;
          &lt;input type=&quot;text&quot; name= &quot;femedu&quot;  id= &quot;femedu&quot; &gt; &lt;br&gt; 
               &lt;/div&gt;`

Java script code

        `&lt;script&gt;
        function openCity(evt, cityName) {
        var i, tabcontent, tablinks;
         tabcontent = document.getElementsByClassName(&quot;tabcontent&quot;);
           for (i = 0; i &lt; tabcontent.length; i++) {
          tabcontent[i].style.display = &quot;none&quot;;
               }
        tablinks = document.getElementsByClassName(&quot;tablinks&quot;);
         for (i = 0; i &lt; tablinks.length; i++) {
            tablinks[i].className = tablinks[i].className.replace(&quot; active&quot;, &quot;&quot;);
              }
            document.getElementById(cityName).style.display = &quot;block&quot;;
             evt.currentTarget.className += &quot; active&quot;;
                  }
                &lt;/script&gt;

             &lt;input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;submit&quot; value=&quot;Submit&quot;&gt;	
                 &lt;/form&gt; `

答案1

得分: 1

您当前的设置会提交表单以及子部分中所有命名输入。您想要避免提交位于非活动选项卡内的字段。一个可能的解决方案是通过在更改选项卡时删除旧项目并添加新项目来更改表单的内部内容,但基于多种原因,我不建议采用这种方法,例如:

  • 您可能已为这些字段实施了一些JavaScript事件,如果更改结构,这些事件将丢失,您将需要重新创建它们。
  • 您已经实现了激活和停用这些选项卡的逻辑,我们希望避免强制您进行根本性的更改。

因此,与其更改结构,让我们更改属性。

每当切换选项卡焦点时,我们希望删除字段内的name属性,并将适当的name属性添加到活动选项卡内的输入字段。

请注意,我已默认设置了第一个选项卡,并且不是使非活动选项卡消失,为了测试的目的,我已对命名输入应用了颜色,换句话说,您随时可以知道如果提交表单,将发送什么到服务器。另请注意,我对您的代码进行了重构,以使其更易于阅读。您可以轻松将此从命名输入的颜色更改为隐藏未命名的属性:

解释逻辑:我们循环遍历具有classtabcontent的元素,并针对每个元素循环遍历具有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(&quot;tabcontent&quot;);
    for (i = 0; i &lt; tabcontent.length; i++) {
        //tabcontent[i].style.display = &quot;none&quot;;
    }
    tablinks = document.getElementsByClassName(&quot;tablinks&quot;);
    for (i = 0; i &lt; tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(&quot; active&quot;, &quot;&quot;);
    }
    evt.currentTarget.classList.add(&quot;active&quot;);
    //document.getElementById(cityName).classList.add(&quot;active&quot;);
    for (let tabcontent of document.getElementsByClassName(&quot;tabcontent&quot;)) {
        for (let input of tabcontent.querySelectorAll(&quot;[data-name]&quot;)) {
            if (tabcontent.id === cityName) {
                input.setAttribute(&quot;name&quot;, input.getAttribute(&quot;data-name&quot;));
            } else {
                input.removeAttribute(&quot;name&quot;);
            }
        }
    }
}

<!-- language: lang-css -->

.active {
    color: green;
}

form [name] {
    background-color: lightgreen;
}

<!-- language: lang-html -->

&lt;form method=&quot;POST&quot; action=&quot;abc.php&quot;&gt;
    &lt;input type=&quot;text&quot; name= &quot;mrno&quot;  id= &quot;mrno&quot; &gt; &lt;br&gt;
    &lt;input type=&quot;text&quot; name= &quot;name1&quot;  id= &quot;name1&quot; &gt; &lt;br&gt;
    &lt;div class=&quot;tab&quot;&gt;
        &lt;button class=&quot;tablinks active&quot; type=&quot;button&quot; onclick=&quot;openCity(event, &#39;London&#39;)&quot;&gt;Value Range          Result&lt;/button&gt;
        &lt;button class=&quot;tablinks&quot; type=&quot;button&quot;onclick=&quot;openCity(event, &#39;Paris&#39;)&quot;&gt;Fixed Test       Result&lt;/button&gt;
    &lt;/div&gt;
    &lt;div id=&quot;London&quot; class=&quot;tabcontent&quot;&gt;
        &lt;input type=&quot;text&quot; name=&quot;maleage&quot; data-name= &quot;maleage&quot;  id= &quot;maleage&quot; &gt; &lt;br&gt;
        &lt;input type=&quot;text&quot; name=&quot;maleedu&quot; data-name= &quot;maleedu&quot;  id= &quot;maleedu&quot; &gt; &lt;br&gt;
    &lt;/div&gt;

    &lt;div id=&quot;Paris&quot; class=&quot;tabcontent&quot;&gt;
        &lt;input type=&quot;text&quot; data-name= &quot;femage&quot;  id= &quot;femage&quot; &gt; &lt;br&gt;
        &lt;input type=&quot;text&quot; data-name= &quot;femedu&quot;  id= &quot;femedu&quot; &gt; &lt;br&gt; 
    &lt;/div&gt;
&lt;/form&gt;

<!-- 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(&quot;tabcontent&quot;);
    for (i = 0; i &lt; tabcontent.length; i++) {
        //tabcontent[i].style.display = &quot;none&quot;;
    }
    tablinks = document.getElementsByClassName(&quot;tablinks&quot;);
    for (i = 0; i &lt; tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(&quot; active&quot;, &quot;&quot;);
    }
    evt.currentTarget.classList.add(&quot;active&quot;);
    //document.getElementById(cityName).classList.add(&quot;active&quot;);
    for (let tabcontent of document.getElementsByClassName(&quot;tabcontent&quot;)) {
        for (let input of tabcontent.querySelectorAll(&quot;[data-name]&quot;)) {
            if (tabcontent.id === cityName) {
                input.setAttribute(&quot;name&quot;, input.getAttribute(&quot;data-name&quot;));
            } else {
                input.removeAttribute(&quot;name&quot;);
            }
        }
    }
}

<!-- language: lang-css -->

.active {
    color: green;
}

form input:not([name]) {
    display: none;
}

<!-- language: lang-html -->

&lt;form method=&quot;POST&quot; action=&quot;abc.php&quot;&gt;
    &lt;input type=&quot;text&quot; name= &quot;mrno&quot;  id= &quot;mrno&quot; &gt; &lt;br&gt;
    &lt;input type=&quot;text&quot; name= &quot;name1&quot;  id= &quot;name1&quot; &gt; &lt;br&gt;
    &lt;div class=&quot;tab&quot;&gt;
        &lt;button class=&quot;tablinks active&quot; type=&quot;button&quot; onclick=&quot;openCity(event, &#39;London&#39;)&quot;&gt;Value Range          Result&lt;/button&gt;
        &lt;button class=&quot;tablinks&quot; type=&quot;button&quot;onclick=&quot;openCity(event, &#39;Paris&#39;)&quot;&gt;Fixed Test       Result&lt;/button&gt;
    &lt;/div&gt;
    &lt;div id=&quot;London&quot; class=&quot;tabcontent&quot;&gt;
        &lt;input type=&quot;text&quot; name=&quot;maleage&quot; data-name= &quot;maleage&quot;  id= &quot;maleage&quot; value=&quot;a&quot; &gt; &lt;br&gt;
        &lt;input type=&quot;text&quot; name=&quot;maleedu&quot; data-name= &quot;maleedu&quot;  id= &quot;maleedu&quot; value=&quot;b&quot; &gt; &lt;br&gt;
    &lt;/div&gt;

    &lt;div id=&quot;Paris&quot; class=&quot;tabcontent&quot;&gt;
        &lt;input type=&quot;text&quot; data-name= &quot;femage&quot;  id= &quot;femage&quot; value=&quot;c&quot; &gt; &lt;br&gt;
        &lt;input type=&quot;text&quot; data-name= &quot;femedu&quot;  id= &quot;femedu&quot; value=&quot;d&quot; &gt; &lt;br&gt; 
    &lt;/div&gt;
&lt;/form&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年7月13日 18:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定