2010年6月3日木曜日

html の select 要素でまたはまる

 html のフォーム系要素は、まだまだ糞だ。IE6 の select が、zindex に関係なく一番上にくるというアホな話は、まあいいとして・・・。今回は、select 要素を readonly にできなくてハマった。正確に言えば、readonly="readonly" と指定しても、ぜんぜん readonly じゃないのだ・・・。バンバン選択できるぜセニョリータ・・・アホけ?

いやいや、disabled がありますよ?

 そうは問屋がおろさないのである。フォームをサブミットした時に、disabled された要素は、もちろん無視される。そうすると、どうなるか? rails とかで、更新時に値がリセットされてしまうのである。こいつを避けるために hidden 要素を追加すればいいのである・・・。である。わかるよ。うん。わかる。hidden 要素を追加すればいいんだよね?アホだろ?

 そもそも、input 要素とかにフィールドと連携した id 属性が自動で割り振られるようになっているところへ、hidden 要素を追加しろだー?なんで、こんなまわりくどい事をせにゃならんねん・・・。

 他にも、style で、ime-mode: active, inactive, ... の中に、カナ・モードとかも欲しいところなのだが、ありま千円。これ最初に考えたの MS でしょ?中途半端だ・・・。せっかく firefox も追随してくれてるのに・・・。

追記: 結局のところ、activescaffold を改造するハメに・・・ form_column_helpers.rb

def active_scaffold_input_singular_association(column, html_options)
associated = @record.send(column.association.name)

select_options = options_for_association(column.association)
select_options.unshift([ associated.to_label, associated.id ]) unless associated.nil? or select_options.find {|label, id| id == associated.id}

selected = associated.nil? ? nil : associated.id
method = column.name
#html_options[:name] += '[id]'
options = {:selected => selected, :include_blank => as_(:_select_)}

html_options.update(column.options[:html_options] || {})
html = ""
# to be disabled ... and add hidden tag
if html_options[:readonly]
html_options[:disabled] = true;
end
options.update(column.options)
html << select(:record, method, select_options.uniq, options, html_options)
if html_options[:readonly]
# この段階でないと、options[:name] が確定していない?
html << "<input type='hidden' name='#{html_options[:name]}' value='#{selected}' />"
end
html
end

くどい・・・

2010/06/18 追記: W3Cでは、readonly なんて属性は invalid のようだ…。ちゃんと練られていないって事でいいのかな?

0 件のコメント: