简体   繁体   中英

HTML select options from json local file

On my HTML page, I want to have two selections, one for picking nationality and another for picking month booking, but there are too many options for the first selection. I managed to get a json file with all the nationalities (looks like this [{"Code":167,"Nationality":" Russia "},{"Code":98,"Nationality":" Ireland "},{"Code":14,"Nationality":" Australia "}, ...) and now I need a simple way to replace my selection options by this. The HTML body is this:

<body onload="initialize()">
    <h1>Hotel Recommendations</h1>
    <h3>The Best Hotels in Europe</h3>

    <div id="currentText">We recommend the best hotels for you, based on your nationality and month of booking. 
        Do you want to give it a try?</div>
    <p></p>

    <form action = "/p/5433/newhotels" method ="POST">
        {% if error_message %}
          <div id="error"><p>{{error_message}}</p></div>
        {% endif %}

        <div id="floating-panel">
            <div>
                <select id="selector" name="nationality">
                    <option value="{{nationality}}" >Nationality</option>
                    <option value="80">Portuguese</option>
                    <option value="60">English</option>
                    <option value="55">Spanish</option>
                    <option value="65">Chinese</option>
                    <option value="30">French</option>
                </select>
            </div>
            <div>
                <select id="selector" name="month">
                    <option value="{{month}}" >Booking Month</option>
                    <option value="01">January</option>
                    <option value="02">February</option>
                    <option value="03">March</option>
                    <option value="04">April</option>
                    <option value="05">May</option>
                    <option value="06">June</option>
                    <option value="07">July</option>
                    <option value="08">August</option>
                    <option value="09">September</option>
                    <option value="10">October</option>
                    <option value="11">November</option>
                    <option value="12">December</option>
                </select>
                <input type="submit" value="Get Hotels">
            </div>
            <div>
                <select id="locality-dropdown" name="locality" onload="placeNationalities()"></select>
            </div>
        </div>
        <div id="all">
            <div id="mapid" style="width: 1200px; height: 600px;"></div>
            <div id="filters">
                <label for="fname">Number of Hotels</label>
                <input type="text" name="numberHotels" value={{numberHotels}} size="5">

                <label for="lname">MinAvg Rating</label>
                <input type="text" name="minAvgRat" value={{minAvgRat}} size="5">
            </div>
        </div>
    </form>
</body>

I also have some

Solved with json content on html! Solution (with json reduced):

<body>
...
<div>
    <select id="selectorr" name="nationality">
    </select>
    <script>
        html = "";
        obj = [{"Code":167,"Nationality":" Russia "},{"Code":98,"Nationality":" Ireland "}]
        for (i=0; i<obj.length; i++) {
            html += "<option value=" + obj[i].Code  + ">" + obj[i].Nationality + "</option>"
        }
        document.getElementById("selectorr").innerHTML = html;
    </script>
</div>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM