2012-10-17 2 views
1

레일에서 루비를 배우고 있습니다. 내 프로젝트에서 나는 collection_select를 사용한다. 내가 대신collection_select onchange 이벤트 및 선택된 루비 레일

<%= collection_select(:sport_name,count,Sport.find(:all, :order => 'id'),:id, :sport_name, 
    {:selected  => ps.sport.id, 
    :include_blank => "Select Sport", 
    :onchange  => "hidvalue("+ps.sport.id.to_s+","+count.to_s+")" }, 
    {:style   => "margin:1px 0 0;width:210px;" }) %> 

onchange 작동하지 않습니다,하지만 selected 일을 할 경우

작동하지 않습니다 selected - 내 코드

<%= collection_select(:sport_name,count,Sport.find(:all, :order => 'id'), :id, :sport_name, {} , 
    {:selected  => ps.sport.id, 
    :include_blank => "Select Sport", 
    :onchange  => "hidvalue("+ps.sport.id.to_s+","+count.to_s+")", 
    :style   => "margin:1px 0 0;width:210px;" }) %> 

onchange 작품이다. onchangeselected을 함께 사용하고 싶습니다. 이 코드의 문제점은 무엇입니까?

+0

수행해야 할 onchange 함수는 무엇입니까? –

+0

숨겨진 필드에 선택한 값을 저장합니다. – tinzawtun

답변

5

"선택"은 옵션이지만 "onchange"는 생성 된 HTML에 할당하려는 HTML 속성입니다. 이 두 가지 다른 유형의 것들은 이고 다른 인수 안에는 collection_select에이 전달된다고 가정합니다.

특히 "선택됨"은 다섯 번째 ("옵션") 해시의 키/값 쌍으로 전달되어야하며 "onchange"는 여섯 번째 ("html_options") 해시의 일부로 전달되어야합니다.

자세한 내용은 http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select을 참조하십시오.

+1

Bob에게 감사드립니다! 이런 식으로 내 코드를 변경합니다. 그것은 작동합니다! id = : sport_name, options = {: include_blank => "스포츠 선택", : 선택한 => ps (스포츠 팀) : \t <% = collection_select (: sports_name, count, Sport.find .sport.id}, html_options = {: onchange => "hidvalue ("+ ps.sport.id.to_s + ","+ count.to_s + ")", : style => "여백 : 1px 0 0, 너비 : 210px ; "}) %> – tinzawtun