向选项添加值 [英] Add a value to the option

查看:104
本文介绍了向选项添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Geddy框架中使用EJS(嵌入式javascript)创建一个select元素.我想做的事情是这样的(只是一个例子):

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

我没有找到任何示例...我知道创建类似这样的选择

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

我必须写

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

但是如何获得我想要的东西?

更新:

我已经在EJS文档中找到了 select_tag ,但是无法识别...我也尝试了混合式

<%- 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'}) %>

但还是一无所有.还有其他想法吗?

解决方案

在新版本中,有一个新的selectTag.它的工作方式类似于您引用的EJS示例,因此您可以传递对象数组.它还允许您指定selectedOption.您仍然可以使用contentTag("select"...)并且它与对象数组一起使用,但是,对于contentTag,您不能选择一个选项.

执行此操作的另一种方法是使用contentTag并将其传递给字符串而不是像这样的数组

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

请查看 geddyjs.org/documentation 上的文档,并查找selectTag

注意:文档会在一夜之间更新,所以当您去那里时它可能不在那儿,我现在正在复制它.

selectTag

`selectTagString(optionsArray,selectedOption,htmlOptions)

使用给定的optionsArray创建HTML选择标签,以创建HTML选项元素.

optionsArray可以是字符串,数字或具有值和文本属性的对象的数组,分别用于值属性和选项元素的内容.

例子:

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>

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?

解决方案

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>

这篇关于向选项添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆