最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to present checkbox values in HTML (using ejs) after extracting data from mongo for User to Edit? - Stack Overf

programmeradmin4浏览0评论

I currently save data into mongo for a multiple choice question and mark the correct answers, In mongo I save them something like question.options.option1.check = "on" for the correct answers.

When I go to the "edit" page I pull the particular record from mongo which is available in ejs as "question" and I am easily able to fill the editable fields values as question.question, or question.options.option1.text etc. But I also want to mark the check boxes that have been previously checked. Below is an example of the checkbox.

<input  id=opt1 type="checkbox" name="check1"  >

when I use <input id=opt1 type="checkbox" name="check1" <%=question.options.option1.check%> > it does not work, I also tried <input id=opt1 type="checkbox" name="check1" checked="<%=question.options.option1.check%>"> this does not work either. But if I give the attribute "checked" = "any value here or no value at all" it checks, which is wrong.

So the question is what is the correct way to check the appropriate checkboxes on the edit page.?

I currently save data into mongo for a multiple choice question and mark the correct answers, In mongo I save them something like question.options.option1.check = "on" for the correct answers.

When I go to the "edit" page I pull the particular record from mongo which is available in ejs as "question" and I am easily able to fill the editable fields values as question.question, or question.options.option1.text etc. But I also want to mark the check boxes that have been previously checked. Below is an example of the checkbox.

<input  id=opt1 type="checkbox" name="check1"  >

when I use <input id=opt1 type="checkbox" name="check1" <%=question.options.option1.check%> > it does not work, I also tried <input id=opt1 type="checkbox" name="check1" checked="<%=question.options.option1.check%>"> this does not work either. But if I give the attribute "checked" = "any value here or no value at all" it checks, which is wrong.

So the question is what is the correct way to check the appropriate checkboxes on the edit page.?

Share Improve this question edited Nov 23, 2016 at 19:31 user856984 asked Nov 23, 2016 at 19:02 user856984user856984 1251 gold badge3 silver badges8 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 18

The input type checkbox takes the attribute checked as true, even if you don't assign any value to it.

So, you may need to do something like

<input id="opt1" type="checkbox" name="check1" <%= question.options.option1.check ? "checked" : "" %> />

Let me know if that worked for you. If the conditional couldn't be evaluated inside the <%= %> ejs tag, you may need to evaluate the conditional and storing in another variable before displaying it:

<% let checked = question.options.option1.check ? "checked" : "" %>
<input id="opt1" type="checkbox" name="check1" <%= checked %> />

You can put it like this too:

  <label class="form-check-label" for="radio2">
         <input type="radio" class="form-check-input" 
             id="rstatus1" 
             name="status"
             value="S" 
            <%= status === 'S' ? 'checked' : '' %> >SIM
  </label>

I had a similar problem. I used the second option given by adonike. Below is what the code block where I used it. The first option sets true all the checkboxes; even if I set the value to false, 'false' or ''. Using the variable unescaped as <%-genChk%> worked too.

<% genres.forEach( g => {%>
  <% let genreChk = g.checked ? 'checked' : '' %>
  <input type="checkbox" name="gen" id="<%=g._id%>" value="<%=g._id%>" <%=genreChk%>>
  <label for="<%=g._id%>"><%=g.name%></label>
<%})%>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论