Add a value to the option

青春壹個敷衍的年華 提交于 2019-12-12 02:07:08

问题


I am trying to create a select element using EJS (embedded javascript) in Geddy framework. What I want to do is have something like (just an example):

<select>
  <option value="int"> Integer </option>
  <option value="float"> Single precision decimal point </option>
</select>

I don't find any examples... I know that for creating a select like

<select>
  <option value="X"> X </option>
  <option value="Y"> Y </option>
</select>

I have to write

<%- contentTag('select', ['X', 'Y']} %>

But how to obtain what I want?

UPDATE:

I've found the select_tag in the EJS documentation, but it is not recognized... I've also tried a hybrid like

<%- contentTag('select', [{value: NumberTypeEL.NonNegativeInteger, text:'nonnegative integer'}, {value: NumberTypeEL.Integer, text: 'integer'}, {value: NumberTypeEL.Decimal, text: 'decimal'}, {value:NumberTypeEL.Fraction, text:'fraction'}], {class:'span6', name:'numberType'}) %>

but still nothing. Any other ideas?


回答1:


On the new version there's a new selectTag. It works like the EJS example you referred to, so you can pass an array of objects. It also lets you specify a selectedOption. You can still use contentTag("select"...) and it works with the array of objects, however, with the contentTag you can't select an option.

Another way to do this that was already there is to use contentTag and pass it a string instead of an array like

contentTag('select', "<option value=\"1\" selected>Text 1</option>");

Please have a look at the documentation on geddyjs.org/documentation and look for selectTag

NOTE: the documentation updates over night, so it might not be there when you go there, I'm copying it for now.

selectTag

`selectTagString(optionsArray, selectedOption, htmlOptions)

Creates a HTML select tag using the given optionsArray to create HTML option elements.

optionsArray could be an array of strings, numbers or an object with value and text properties to be used for the value attribute and option element content respectively.

Examples:
selectTag(['geddy', 'alex', 'neil'])
// => '<select><option value="geddy">geddy</option><option value="alex">alex</option><option value="neil">neil</option></select>'

selectTag(['open', 'close'], todo.status, { class:'span6', name:'status' })
// => '<select class="span6" name="status"><option selected="selected" value="open">open</option><option value="close">close</option></select>'

selectTag([{value: 1, text: "Text 1"}, {value: 2, text: "Text 2"}], 2)
// => <select><option value="1">Text 1</option><option selected="selected" value="2">Text 2</option></select>


来源:https://stackoverflow.com/questions/15316058/add-a-value-to-the-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!