开发者

Capturing Rails 3 Multiselect Input Params

I have a multiselect form control (fig. 1). When I select more than 1 value, Firefox sends both values (fig. 2). But only the last value gets sent as a input value to my controller (fig. 3). How do I get all those values passed on to my controller?

  <form action="html_items/search" method="post" >

    <!-- Criteria --> 
    <div style="float:none;">
      <label>Content Provider </label>
      <select multiple id="content_provider" name="content_provider">
        <option value="FLR">Flare</option>
        <option value="SLDT">Slashdot</option>
      </select>
    </div>

    <input type="submit" value="Search" />

  </form>

fig. 1

Parametersapplication/x-www-form-urlencoded

content_provider FLR

content_provider SLDT

fig. 2

PARAMs[{"content_provider"=>"SLDT", "controller"=>"html_items", "action"=>"search"}]

...

开发者_StackOverflow社区fig. 3

Thanks

Tim


The first thing is that if you want to create an array of the elements that are selected you need to name your <select> something like <select name='foo[]'>

Next... If nothing is selected nothing will show up. This makes sense, but it stumped me for a bit. I was doing ajax where the left select was search results and the right was the completed list.

I just added a .click(function(){}); to my submit button to set every item on the left to selected with jQuery and the items showed up in the parameters as an array. In the below example you should see the first item show up.

<select multiple id="content_provider" name="content_provider">
        <option value="FLR" selected>Flare</option>
        <option value="SLDT">Slashdot</option>
</select>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜