Bootstrap单选按钮在Flask中无法正常工作。

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

Bootstrap radio button isn't working in flask

问题

我在一个Flask应用程序中使用了Bootstrap 4的单选按钮[单选按钮][1],以下是我使用的代码片段:

<div class="btn-group btn-group-toggle" data-toggle="buttons">
    <label class="btn btn-outline-secondary">
        <input type="radio" onclick="javascript:toggleElements();" name="toggle" id="twitter"/> Twitter
    </label>

    <label class="btn btn-outline-secondary">
        <input type="radio" onclick="javascript:toggleElements();" name="toggle" id="facebook"/> Facebook
    </label>
</div>

还有JavaScript代码:

<script type="text/javascript">
    function toggleElements() {
        alert('hurray');
        if (document.getElementById('twitter').checked) {
            document.getElementById('twitterrows').style.display = 'block';
            document.getElementById('facebookrows').style.display = 'none';
        }

        if (document.getElementById('facebook').checked) {
            document.getElementById('facebookrows').style.display = 'block';
            document.getElementById('twitterrows').style.display = 'none';
        }
    }
</script>

最初,我尝试使用简单的HTML单选按钮,并能够触发JavaScript函数toggleElements()。然而,在将普通单选按钮替换为Bootstrap单选按钮后,没有触发任何操作。

以下是整个HTML代码:

{% from 'helper/forms.html' import render_field with context %}
<!DOCTYPE html>
<html>

<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sharjah Tourism Search Results</title>

    <script src="{{ url_for('static', filename='js/jquery-3.2.1.min.js') }}"></script>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="{{ url_for('static',filename='styles/home.css')}}" />

    <script type="text/javascript">
        function toggleElements() {
            alert('hurray');
            if (document.getElementById('twitter').checked) {
                document.getElementById('twitterrows').style.display = 'block';
                document.getElementById('facebookrows').style.display = 'none';
            }

            if (document.getElementById('facebook').checked) {

                document.getElementById('facebookrows').style.display = 'block';
                document.getElementById('twitterrows').style.display = 'none';
            }
        }
    </script>
</head>

<body>

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="{{ url_for('home')}}"><strong>Search</strong></a>

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent-7"
            aria-controls="navbarSupportedContent-7" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent-7">
            <ul class="navbar-nav mr-auto">

                <li class="nav-item">
                    <a class="nav-link" href="{{url_for('add_url')}}">Add<span class="sr-only">(current)</span></a>
                </li>

                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('file_render')}}">Import</a>
                </li>

                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
                        Websites
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="{{ url_for('list_urls') }}">List</a>
                        <a class="dropdown-item" href="{{ url_for('list_crawled_websites')}}">Selected</a>
                    </div>
                </li>
            </ul>
        </div>
    </nav>

    <!-- Header -->
    <div class="container">
        <br />
        <div class="row">
            <!-- Logo -->
            <div class="col-sm">
                <a href="#"><img class="w-50 h-60"
                        src="{{url_for('static',filename='images/sctda_logo.jpg')}}" /></a>
            </div>

            <!-- Search Bar -->
            <div class="col-sm-5 searchbar">
                <form action="{{url_for('fetch_results')}}" method="POST">
                    <div class="input-group">
                        <input type="search" placeholder="Search" class="form-control" name="search" id="search"
                            value="{{data['search_term']}}" class="w-75 h-75" required>
                        <div class="input-group-append">
                            <button type="submit" class="btn btn-outline-secondary btn-sm"><i
                                    class="fa fa-search"></i></button>
                        </div>
                    </div>
                </form>
            </div>

        </div>


        <div id="container">
            <div class="row">
                {%set data = data['results'] %}
                <div class="col-md-4">
                    <div class="btn-group-toggle" data-toggle="buttons">
                        <label class="btn btn-outline-secondary active">
                            <input type="radio" checked autocomplete="off"> Internal
                        </label>
                    </div>
                    <hr>

                    {% set internals = data['internal'] %}
                    {% if internals is defined and internals|length %}
                    {% for internal in internals %}
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">{{ internal['title'] }}</h5>
                            <p class="card-text">{{ internal['description'] }}</p>
                            <a href="{{ internal['url'] }}" class="card-link">{{ internal['title'] }}</a>
                        </div>
                    </div>
                    <br/>
                    {% endfor %}
                    {% else %}
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">No Records Found</h5>
                        </div>
                    </div>
                    <br/>
                    {% endif %}
                </div>

                <div class="col-md-4">
                    {% set externals = data['external'] %}
                    {% if externals is defined and externals|length %}
                    <div class="btn-group-toggle" data-toggle="buttons">
                        <label class="btn btn-outline-secondary active">
                            <input type="radio" checked autocomplete="off"> External
                        </label>
                    </div>
                    <hr>
                    {% for external in externals %}
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">{{ external['title'] }}</h5>


<details>
<summary>英文:</summary>

I am using Bootstrap 4 radio button [radio button][1] in a flask application. and below is the snippet I used 

<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-outline-secondary">
<input type="radio" onclick="javascript:toggleElements();" name="toggle" id="twitter"/> Twitter
</label>

&lt;label class=&quot;btn btn-outline-secondary&quot;	&gt;
&lt;input type=&quot;radio&quot; onclick=&quot;javascript:toggleElements();&quot; name=&quot;toggle&quot; id=&quot;facebook&quot;/&gt; Facebook
&lt;/label&gt;

</div>


and the javascript code 
&lt;script type=&quot;text/javascript&quot;&gt;
function toggleElements() {
alert(&#39;hurray&#39;);
if(document.getElementBId(&#39;twitter&#39;).checked) {
document.getElementById(&#39;twitterrows&#39;).style.display = &#39;block&#39;;
document.getElementById(&#39;facebookrows&#39;).style.display = &#39;none&#39;;
}
if(document.getElementById(&#39;facebook&#39;).checked) {
document.getElementById(&#39;facebookrows&#39;).style.display = &#39;block&#39;;
document.getElementById(&#39;twitterrows&#39;).style.display = &#39;none&#39;;
}
}
&lt;/script&gt;
Initially,I tried with simple html radio buttons and was able to trigger Javascript function toggleElements(). However, after replacing the normal radio buttons with the bootstrap radio, nothing is triggered.
Below is the entire html code
-----

{% from 'helper/forms.html' import render_field with context %}
<!DOCTYPE html>
<html>

<head>

&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Sharjah Tourism Search Results&lt;/title&gt;
&lt;script src=&quot;{{ url_for(&#39;static&#39;, filename=&#39;js/jquery-3.2.1.min.js&#39;) }}&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot;&gt;
&lt;!-- &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt; --&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&#39;static&#39;,filename=&#39;styles/home.css&#39;)}}&quot;/&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function toggleElements() {
alert(&#39;hurray&#39;);
if(document.getElementBId(&#39;twitter&#39;).checked) {
document.getElementById(&#39;twitterrows&#39;).style.display = &#39;block&#39;;
document.getElementById(&#39;facebookrows&#39;).style.display = &#39;none&#39;;
}
if(document.getElementById(&#39;facebook&#39;).checked) {
document.getElementById(&#39;facebookrows&#39;).style.display = &#39;block&#39;;
document.getElementById(&#39;twitterrows&#39;).style.display = &#39;none&#39;;
}
}
&lt;/script&gt;

</head>

<body>

&lt;nav class=&quot;navbar navbar-expand-lg navbar-dark bg-dark&quot;&gt;
&lt;a class=&quot;navbar-brand&quot; href=&quot;{{ url_for(&#39;home&#39;)}}&quot;&gt;&lt;strong&gt;Search&lt;/strong&gt;&lt;/a&gt;
&lt;button class = &quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarSupportedContent-7&quot;

aria-controls="navbarSupportedContent-7" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

	&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarSupportedContent-7&quot;&gt;
&lt;ul class=&quot;navbar-nav mr-auto&quot;&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;{{url_for(&#39;add_url&#39;)}}&quot;&gt;Add&lt;span class=&quot;sr-only&quot;&gt;(current)&lt;/span&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt;
&lt;a class=&quot;nav-link&quot; href=&quot;{{ url_for(&#39;file_render&#39;)}}&quot;&gt;Import&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;nav-item dropdown&quot;&gt;
&lt;a class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; id=&quot;navbardrop&quot; data-toggle=&quot;dropdown&quot;&gt;
Websites
&lt;/a&gt;
&lt;div class=&quot;dropdown-menu&quot;&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;{{ url_for(&#39;list_urls&#39;) }}&quot;&gt;List&lt;/a&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;{{ url_for(&#39;list_crawled_websites&#39;)}}&quot;&gt;Selected&lt;/a&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;!-- Header --&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;br /&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;!-- Logo --&gt;
&lt;div class=&quot;col-sm&quot;&gt;
&lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;w-50 h-60&quot; src=&quot;{{url_for(&#39;static&#39;,filename=&#39;images/sctda_logo.jpg&#39;)}}&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;!-- Search Bar --&gt;
&lt;div class=&quot;col-sm-5 searchbar&quot;&gt;
&lt;form action=&quot;{{url_for(&#39;fetch_results&#39;)}}&quot; method=&quot;POST&quot;&gt;
&lt;div class=&quot;input-group&quot;&gt;
&lt;input type=&quot;search&quot; placeholder=&quot;Search&quot; class=&quot;form-control&quot; name=&quot;search&quot; id=&quot;search&quot; value=&quot;{{data[&#39;search_term&#39;]}}&quot; class=&quot;w-75 h-75&quot; required&gt;
&lt;div class=&quot;input-group-append&quot;&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-outline-secondary btn-sm&quot;&gt;&lt;i class=&quot;fa fa-search&quot;&gt;&lt;/i&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;container&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
{%set data = data[&#39;results&#39;] %}
&lt;div class=&quot;col-md-4&quot;&gt;
&lt;div class=&quot;btn-group-toggle&quot; data-toggle=&quot;buttons&quot;&gt;
&lt;label class=&quot;btn btn-outline-secondary active&quot;&gt;
&lt;input type=&quot;radio&quot; checked autocomplete=&quot;off&quot;&gt; Internal
&lt;/label&gt;
&lt;/div&gt;
&lt;hr&gt;
{% set internals = data[&#39;internal&#39;] %}
{% if internals is defined and internals|length %}
{% for internal in internals %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;{{ internal[&#39;title&#39;] }}&lt;/h5&gt;
&lt;p class=&quot;card-text&quot;&gt;{{ internal[&#39;description&#39;] }}&lt;/p&gt;
&lt;a href=&quot;{{ internal[&#39;url&#39;] }}&quot; class=&quot;card-link&quot;&gt;{{ internal[&#39;title&#39;] }}&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endfor %}
{% else %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;No Records Found&lt;/h5&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endif %}
&lt;/div&gt;
&lt;div class=&quot;col-md-4&quot;&gt;
{% set externals = data[&#39;external&#39;] %}
{% if externals is defined and externals|length %}
&lt;div class=&quot;btn-group-toggle&quot; data-toggle=&quot;buttons&quot;&gt;
&lt;label class=&quot;btn btn-outline-secondary active&quot;&gt;
&lt;input type=&quot;radio&quot; checked autocomplete=&quot;off&quot;&gt; External
&lt;/label&gt;
&lt;/div&gt;
&lt;hr&gt;
{% for external in externals %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;{{ external[&#39;title&#39;] }}&lt;/h5&gt;
&lt;p class=&quot;card-text&quot;&gt;{{ external[&#39;description&#39;] }}&lt;/p&gt;
&lt;a href=&quot;{{ external[&#39;url&#39;] }}&quot; class=&quot;card-link&quot;&gt;{{ external[&#39;title&#39;] }}&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endfor %}
{% else %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;No Records Found&lt;/h5&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endif %}
&lt;/div&gt;
&lt;div class=&quot;col-md-4&quot;&gt;
&lt;div class=&quot;btn-group btn-group-toggle&quot; data-toggle=&quot;buttons&quot;&gt;
&lt;label class=&quot;btn btn-outline-secondary&quot;&gt;
&lt;input type=&quot;radio&quot; onclick=&quot;javascript:toggleElements();&quot; name=&quot;toggle&quot; id=&quot;twitter&quot;/&gt; Twitter
&lt;/label&gt;
&lt;label class=&quot;btn btn-outline-secondary&quot;	&gt;
&lt;input type=&quot;radio&quot; onclick=&quot;javascript:toggleElements();&quot; name=&quot;toggle&quot; id=&quot;facebook&quot;/&gt; Facebook
&lt;/label&gt;
&lt;/div&gt;
&lt;hr&gt;
(&lt;input type=&quot;radio&quot; onclick=&quot;javascript:toggleElements();&quot; name=&quot;toggle&quot; id=&quot;twitter&quot;/&gt; Twitter,
&lt;input type=&quot;radio&quot; onclick=&quot;javascript:toggleElements();&quot; name=&quot;toggle&quot; id=&quot;facebook&quot;/&gt; Facebook)
&lt;div id=&quot;twitterrows&quot;&gt;
{% set tweets = data[&#39;twitter&#39;] %}
{% if tweets is defined and tweets|length %}
{% for tweet in tweets %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;{{ tweet[&#39;title&#39;] }}&lt;/h5&gt;
&lt;p class=&quot;card-text&quot;&gt;{{ tweet[&#39;description&#39;] }}&lt;/p&gt;
&lt;a href=&quot;{{ tweet[&#39;url&#39;] }}&quot; class=&quot;card-link&quot;&gt;{{ tweet[&#39;title&#39;] }}&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endfor %}
{% else %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;No Records Found&lt;/h5&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endif %}
&lt;/div&gt;
&lt;div id=&quot;facebookrows&quot;&gt;
{% set posts = data[&#39;facebook&#39;] %}
{% if posts is defined and posts|length %}
{% for post in posts %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;{{ post[&#39;title&#39;] }}&lt;/h5&gt;
&lt;p class=&quot;card-text&quot;&gt;{{ post[&#39;description&#39;] }}&lt;/p&gt;
&lt;a href=&quot;{{ post[&#39;url&#39;] }}&quot; class=&quot;card-link&quot;&gt;{{ post[&#39;title&#39;] }}&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endfor %}
{% else %}
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;No Records Found&lt;/h5&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
{% endif %}
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;center&gt;Developed by School of Information Technology Team,Skyline University College&lt;/center&gt;

</body>
</html>


**I strongly suspect with the order of scripts being called under the head tag**. Can someone help me to fix this please.
[1]: https://getbootstrap.com/docs/4.0/components/buttons/#checkbox-and-radio-buttons
</details>
# 答案1
**得分**: 1
1. 请确保您已经包含了正确版本的bootstrap。请检查浏览器控制台是否有任何错误。
2. https://getbootstrap.com/docs/4.4/components/input-group/#checkboxes-and-radios。
3. 我怀疑JavaScript的点击事件没有正确绑定。
<details>
<summary>英文:</summary>
1. make sure you have included the correct version of bootstrap. Please check if there is any error in the browser console.
2. https://getbootstrap.com/docs/4.4/components/input-group/#checkboxes-and-radios.
3. I am suspecting that the javascript click is not properly binded to the click event.
</details>
# 答案2
**得分**: 0
我通过用以下内容替换上述HTML页面中的三个脚本标签来修复了它:
```html
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
英文:

I fixed it by replacing the three script tags in the above html page with the below

&lt;script src=&quot;https://code.jquery.com/jquery-3.4.1.slim.min.js&quot; integrity=&quot;sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js&quot; integrity=&quot;sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js&quot; integrity=&quot;sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

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

发表评论

匿名网友

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

确定