从json本地文件中选择HTML选项

时间:2018-06-03 15:23:06

标签: javascript html json

在我的HTML页面上,我希望有两个选项,一个用于选择国籍,另一个用于选择月份预订,但是第一个选择的选项太多了。我设法获得了一个包含所有国籍的json文件(看起来像这样 [{“代码”:167,“国籍”:“俄罗斯”},{“代码”:98,“国籍”:“爱尔兰”} ,{“代码”:14,“国籍”:“澳大利亚”}, ...)现在我需要一种简单的方法来替换我的选择选项。 HTML正文如下:

<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>

我也有一些

1 个答案:

答案 0 :(得分:0)

解决了html上的json内容!解决方案(减少了json):

<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>
...