Bootstrap HTML – Tabs as Radio Buttons Forms

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

Bootstrap HTML - Tabs as Radio Buttons Forms

问题

我有一个正在编辑的Bootstrap模板。我正在处理一个表单页面,但似乎无法获取选定的单选按钮,实际上它是一个标签列表,要将其POST到PHP。

<div class="from-step-content">
    <ul class="nav nav-tabs nav-tabs-bordered" role="tablist" name="selected-item">
        <li class="nav-item">
            <a class="nav-link active" data-toggle="tab" href="#item-one">
                <div class="nav-tabs-icon">
                    <img src="images/1.png" alt="icon">
                    <img src="images/1-color.png" alt="icon">
                </div>
                <span>Item One</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#item-two">
                <div class="nav-tabs-icon">
                    <img src="images/2.png" alt="icon">
                    <img src="images/2-color.png" alt="icon">
                </div>
                <span>Item Two</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#item-three">
                <div class="nav-tabs-icon">
                    <img src="images/3.png" alt="icon">
                    <img src="images/3-color.png" alt="icon">
                </div>
                <span>Item Three</span>
            </a>
        </li>
    </ul>
</div>

这是将使用POST方法提交到PHP脚本的方式,但我无法弄清楚如何获取实际选择了哪个项目。有什么想法吗?谢谢!

英文:

I have a bootstrap template that I am currently editing. I'm dealing with a form page, but I cannot seem to get the selected Radio Button, that is actually a tablist to POST to PHP

&lt;div class=&quot;from-step-content&quot;&gt;
        &lt;ul class=&quot;nav nav-tabs nav-tabs-bordered&quot; role=&quot;tablist&quot; name=&quot;selected-item&quot;&gt;
            &lt;li class=&quot;nav-item&quot;&gt;
                &lt;a class=&quot;nav-link active&quot; data-toggle=&quot;tab&quot; href=&quot;#item-one&quot;&gt;
                    &lt;div class=&quot;nav-tabs-icon&quot;&gt;
                        &lt;img src=&quot;images/1.png&quot; alt=&quot;icon&quot;&gt;
                        &lt;img src=&quot;images/1-color.png&quot; alt=&quot;icon&quot;&gt;
                    &lt;/div&gt;
                    &lt;span&gt;Item One&lt;/span&gt;
                &lt;/a&gt;
            &lt;/li&gt;
            &lt;li class=&quot;nav-item&quot;&gt;
                &lt;a class=&quot;nav-link&quot; data-toggle=&quot;tab&quot; href=&quot;#item-two&quot;&gt;
                    &lt;div class=&quot;nav-tabs-icon&quot;&gt;
                        &lt;img src=&quot;images/2.png&quot; alt=&quot;icon&quot;&gt;
                        &lt;img src=&quot;images/2-color.png&quot; alt=&quot;icon&quot;&gt;
                    &lt;/div&gt;
                    &lt;span&gt;Item Two&lt;/span&gt;
                &lt;/a&gt;
            &lt;/li&gt;
            &lt;li class=&quot;nav-item&quot;&gt;
                &lt;a class=&quot;nav-link&quot; data-toggle=&quot;tab&quot; href=&quot;#item-three&quot;&gt;
                    &lt;div class=&quot;nav-tabs-icon&quot;&gt;
                        &lt;img src=&quot;images/3.png&quot; alt=&quot;icon&quot;&gt;
                        &lt;img src=&quot;images/3-color.png&quot; alt=&quot;icon&quot;&gt;
                    &lt;/div&gt;
                    &lt;span&gt;Item Three&lt;/span&gt;
                &lt;/a&gt;
            &lt;/li&gt;
        &lt;/ui&gt;
    &lt;/div&gt;

This is to use method of POST into a PHP script, however I cannot figure out how I actually get which item has indeed been selected. Any ideas? Thanks

答案1

得分: 0

你应该使用JavaScript来处理不可见的单选框输入,因为你无法将HTML DOM发送到PHP,所以需要在表单中进行操作。

因此,为了快速帮助你:

<form action="post.php" method="post">
    <div class="from-step-content">
        <ul class="nav nav-tabs nav-tabs-bordered" role="tablist" name="selected-item">
            <li class="nav-item">
                <a class="nav-link active" data-toggle="tab" href="#item-one">
                    <input type="radio" name="item" value="1" style="display:none;"/> 
                    <div class="nav-tabs-icon">
                        <img src="images/1.png" alt="icon">
                        <img src="images/1-color.png" alt="icon">
                    </div>
                    <span>Item One</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#item-two">
                    <input type="radio" name="item" value="1" style="display:none;"/>
                    <div class="nav-tabs-icon">
                        <img src="images/2.png" alt="icon">
                        <img src="images/2-color.png" alt="icon">
                    </div>
                    <span>Item Two</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#item-three">
                    <input type="radio" name="item" value="1" style="display:none;"/>
                    <div class="nav-tabs-icon">
                        <img src="images/3.png" alt="icon">
                        <img src="images/3-color.png" alt="icon">
                    </div>
                    <span>Item Three</span>
                </a>
            </li>
        </ul>
    </div>
    <button>Submit</button>
</form>

<script>
    $(document).ready(function(){
        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            $('input[type="radio"]').prop("checked", false);
            $(e.target).find('input[type="radio"]').prop("checked", true);
        })
    });
</script>

这是你提供的HTML代码的翻译部分。

英文:

You should use javascript for invisble radio box input, cause you can't send to PHP HTML DOM, so you need to make manipulation with form.

So for quick help:

&lt;form action=&quot;post.php&quot; method=&quot;post&quot;&gt;
&lt;div class=&quot;from-step-content&quot;&gt;
&lt;ul class=&quot;nav nav-tabs nav-tabs-bordered&quot; role=&quot;tablist&quot; name=&quot;selected-item&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link active&quot; data-toggle=&quot;tab&quot; href=&quot;#item-one&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;item&quot; value=&quot;1&quot; style=&quot;display:none;&quot;/&gt; 
&lt;div class=&quot;nav-tabs-icon&quot;&gt;
&lt;img src=&quot;images/1.png&quot; alt=&quot;icon&quot;&gt;
&lt;img src=&quot;images/1-color.png&quot; alt=&quot;icon&quot;&gt;
&lt;/div&gt;
&lt;span&gt;Item One&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; data-toggle=&quot;tab&quot; href=&quot;#item-two&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;item&quot; value=&quot;1&quot; style=&quot;display:none;&quot;/&gt;
&lt;div class=&quot;nav-tabs-icon&quot;&gt;
&lt;img src=&quot;images/2.png&quot; alt=&quot;icon&quot;&gt;
&lt;img src=&quot;images/2-color.png&quot; alt=&quot;icon&quot;&gt;
&lt;/div&gt;
&lt;span&gt;Item Two&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; data-toggle=&quot;tab&quot; href=&quot;#item-three&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;item&quot; value=&quot;1&quot; style=&quot;display:none;&quot;/&gt;
&lt;div class=&quot;nav-tabs-icon&quot;&gt;
&lt;img src=&quot;images/3.png&quot; alt=&quot;icon&quot;&gt;
&lt;img src=&quot;images/3-color.png&quot; alt=&quot;icon&quot;&gt;
&lt;/div&gt;
&lt;span&gt;Item Three&lt;/span&gt;
&lt;/a&gt;
&lt;/li&gt;
&lt;/ui&gt;
&lt;/div&gt;
&lt;button&gt;Submit&lt;/button&gt;
&lt;/form&gt;
&lt;script&gt;
$(document).ready(function(){
$(&#39;a[data-toggle=&quot;tab&quot;]&#39;).on(&#39;shown.bs.tab&#39;, function (e) {
$(&#39;input[type=&quot;radio&quot;]&#39;).prop(&quot;checked&quot;, false);
$(e.target).find(&#39;input[type=&quot;radio&quot;]&#39;).prop(&quot;checked&quot;, true);
})
});
&lt;/script&gt;

huangapple
  • 本文由 发表于 2020年1月3日 21:58:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579843.html
匿名

发表评论

匿名网友

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

确定