html 简单动态下拉(马来西亚国家)使用jQuery //演示:http://jsfiddle.net/qwv7vLvy/

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 简单动态下拉(马来西亚国家)使用jQuery //演示:http://jsfiddle.net/qwv7vLvy/相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Dynamic Dropdown</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>
    <p>Please select your region and state:</p>
    <select id="region">
        <option id="please_select">-- Select Region --</option>
        <option id="wm">West Malaysia</option>
        <option id="em">East Malaysia</option>
    </select>
    <select id="state"></select>
    <br/>
    <br/>
    <button id="showResult">Show Result</button>
    <br/>
    <br/>
    <div id="result"></div>

    <script type="text/javascript">
    // Launch this when file was fully loaded
    $(document).ready(function() {

        // Hide states dropdown
        $("#state,#showResult").hide();

        // Put all West Malaysia states in a variable
        var wmStates = "<option>Perlis</option><option>Kedah</option><option>Perak</option><option>Pulau Pinang</option><option>Selangor</option><option>Melaka</option><option>Negeri Sembilan</option><option>Johor</option><option>Pahang</option><option>Terengganu</option><option>Kelantan</option>";

        // Put all East Malaysia states in a variable
        var emStates = "<option>Sarawak</option><option>WP Labuan</option><option>Sabah</option>";

        // Listen to #region dropdown change
        $("#region").change(function() {

            // Get the selected value
            var selected = $(this).val();

            // Show states dropdown
            $("#state,#showResult").show();

            // If West Malaysia selected, show West Malaysia states
            if (selected == 'West Malaysia') {
                // Show dropdown set for West Malaysia
                $("#state").html(wmStates);
            }
            // If East Malaysia selected, show East Malaysia states
            else if (selected == 'East Malaysia') {
                // Show dropdown set for East Malaysia
                $("#state").html(emStates);
            }
            // If -- Region -- is selected
            else if (selected == '-- Select Region --') {
                // Hide #states, button & #result
                $("#state,#showResult,#result").hide();

                // Ask user to select a region
                alert('Please select your region');
            }
        });

        // Listen to button click
        $("#showResult").click(function() {

            // Get selected value and put in variables
            var regionSelected = $("#region").val();
            var stateSelected = $("#state").val();

            // Put the variables inside the #result div
            $("#result").html("Region: " + regionSelected + "<br/>State: " + stateSelected);
        });

    });
    </script>
</body>

</html>

以上是关于html 简单动态下拉(马来西亚国家)使用jQuery //演示:http://jsfiddle.net/qwv7vLvy/的主要内容,如果未能解决你的问题,请参考以下文章

哪些国家过春节?

Codeigniter 上的动态相关下拉列表国家和州

马来西亚服务器怎么样东南亚国家哪里的服务器比较好

如何在选择时使用 formArray 索引加载动态下拉数据

Laravel 动态下拉国家和州

如何指定仅适用于特定国家/地区的地图? [复制]