On Feb 6, 2006, at 3:24 PM, sunh11373 wrote:

I tried to create a selection using following code:

SELECT({"name":"color_id"},
   OPTION({"value":"red", "selected":compare("red",
result['color'])==0}, "Red"),
   OPTION({"value":"blue", "selected":compare("blue",
result['color'])==0}, "Blue")
)

assume result['color']) == 'red'

The result is:

<select name="color_id">
<option selected="true" value="red">Red</option>
<option selected="false" value="blue">Blue</option>
</select>

which does not work.

Is there a convenient way to generate following code:

<select name="color_id">
<option selected value="red">Red</option>
<option value="blue">Blue</option>
</select>

Not particularly, you'd have to write a function to encapsulate the behavior you want.. Something like this:

var result = {color: "red"};
var opt = function (value, selected/* ... */) {
        var attrs = {value: value};
        if (selected) { attrs.selected = "selected"; }
        return OPTION.apply(null, extend([attrs], arguments, 2));
}

var sel = SELECT({name: "color_id"},
        opt("red", result.color == "red", "Red"),
        opt("blue", result.color == "blue", "Blue")
)

// writeln(toHTML(sel))

Which will produce this:
<select name="color_id">
        <option selected="selected" value="red">Red</option>
        <option value="blue">Blue</option>
</select>

-bob

Reply via email to