0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

BootstrapでValueつきDropdown

Last updated at Posted at 2020-12-02

##例

HTML

<div class="dropdown">
        <button class="btn btn-secondary dropdown-toggle" id="gender" type="button" data-toggle="dropdown">
            性別
        </button>
        <div class="dropdown-menu">
            <button class="dropdown-item" value="male">男性</button>
            <button class="dropdown-item" value="female">女性</button>
        </div>
</div>

Javascript

$(function () {
    $('.dropdown-menu .dropdown-item').click(function () {
        var visibleItem = $('.dropdown-toggle', $(this).closest('.dropdown'));
        visibleItem.text($(this).text());
        visibleItem.attr('value', $(this).attr('value'))
    });
});

項目はdropdown-menu以下に追加してください。

選択項目をJavascriptで取得する場合は
上の例だと

$("#gender").attr('value')//value
$("#gender").text()//text

で取得できます。

参考

Bootstrap4ドロップダウンで選択した項目をボタンに表示したい!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?