LoginSignup
1
1

More than 1 year has passed since last update.

【JavaScript】【jQuery】ドロップダウン選択時その項目のvalue値をhidden項目のvalue値に渡す。

Last updated at Posted at 2021-09-08

例)
下記ドロップダウンで
「東京」(value: "tokyo")を選択するとき、
hidden項目のvalueに "tokyo" を渡す。

index.html
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>

<input id="result" type="hidden" value="">

<select name="都道府県" id="pref">
    <option value="osaka">大阪</option>
    <option value="tokyo">東京</option>
</select>

<script>
    pref = jQuery("#pref")
    pref.on("change", function(){
        var result = $(this).val()

        $("#result").val(result)
        pref_result = document.getElementById("result")
        console.log(pref_result.value) //tokyo
    })
</script>
1
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
1
1