Form Select option and respond to correspond another form selector
now i looking for solution on how to make a dro开发者_如何学JAVAp down select form that will have difference respond to corresponding selection...
for example i make a drop down selector for occupation... when i choose information technology then it will show another selector which is specify to show only when the user choose informaion technology...that popup selector allow us to choose everything related like web design and so on... may i know how can i do it with rails ?I would use some sort of DHTML solution here. You don't actually depend on Rails for the sollution.
Suppose that you have 2 categories of occupation which are "IT Stuff", and "Flower Arranging". Each one has a sub-category.
The form in your view (xxx.erb.html) will look something like this:
<form>
<select name="profession" onChange="makeVisible(this.options[selectedIndex].value);">
<option value="itstuff">IT Stuff</option>
<option value="flowerarranging">Flower Arranging</option>
</select>
<div id="itstuff" style="display:none" >
<!-- Here is where you put the <select> for the sub-categories of IT Stuff -->
<select name="itsub">
<option value="WindowsAdmin">Windows</option>
<option value="NeXTStepAdmin">NeXT</option>
</select>
</div>
<div id="flowerarranging" style="display:none">
<!-- Here is where you put the <select> for the sub-categories of IT Stuff -->
<select name="flowersub">
<option value="roses">Roses</option>
<option value="daisies">Daisies</option>
</select>
</div>
<input type="submit" value="Submit Profession INfo" />
</form>
<script>
function makeAllInvisible() {
document.getElementById('itstuff').style.display='none';
document.getElementById('flowerarranging').style.display='none';
}
function makeVisible(id) {
makeAllInvisible();
document.getElementById(id).style.display='block';
}
</script>
Once you have that, your form handler will need to be able to handle all of the inputs of the form, including the ones in the invisible sections. In Rails-speak, that means that you are going to have to handle params[:profession], params[:itsub], and params[:flowersub] for all submissions of this form.
Be careful of using Rails form helpers here. I have not found a good Rails form tag for select tags within Rails form_for helpers. I usually code them by hand.
精彩评论