简体   繁体   中英

How to show the selected option in a dropdown menu, with the data out mongoDB?

I have a form with is connected to a Mongo Database.

If I fill the form and send it, it goes to the Mongo database. It shows good on the show page.

Now I have made a edit form. So you can edit the data. The input fields (like E-Mail and Bericht) haven't problems to show it.

But the dropdown menu (select and option tags) won't show the selected text what was entered. It shows the standard text (first option).

I thought I could do it with <%= blog.typemelding %> like the same as "email" and "bericht".

This is my show form:

            <p><b>E-Mail adres:</b> <%- blog.email %></p>
            <p><b>Type melding:</b> <%- blog.typemelding %></p>
            <p><b>Bericht:<br></b> <%- blog.body %></p><br>

and this is my edit form:

    <div class="field">
        <label>Email</label>
        <input type="text" name="blog[email]" value=<%= blog.email %>> 
    </div>
    <div class="field" id="melding">
        <label>Type melding</label>
        <select name="blog[typemelding]"><%= blog.typemelding %>
            <option value="----">Kies een melding</option>
            <option value="Onderhoud">Onderhoud</option>
            <option value="Geluidsoverlast">Geluids overlast</option>
        </select>
    </div>

    <div class="field">
        <label>Bericht</label>
            <textarea name="blog[body]"> <%= blog.body%> </textarea>  
    </div>

I think you are using blog.typemelding as a select option value. I hope the following code will help you.

<div class="field">
    <label>Email</label>
    <input type="text" name="blog[email]" value=<%= blog.email %>>
</div>
<div class="field" id="melding">
    <label>Type melding</label>
    <select name="blog[typemelding]">
        <option value="----"  <% if (blog.typemelding == "----"){ %> selected <% } %>>Kies een melding</option>
        <option value="Onderhoud" <% if (blog.typemelding == "Onderhoud"){ %> selected <% } %>>Onderhoud</option>
        <option value="Geluidsoverlast" <% if (blog.typemelding == "Geluidsoverlast"){ %> selected <% } %>>Geluids overlast</option>
    </select>
</div>

<div class="field">
    <label>Bericht</label>
    <textarea name="blog[body]"> <%= blog.body%> </textarea>
</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