2012-04-10 09:47
浏览 67


I've built a dynamically populated multiselect form for my site. When the user selects one or more items in the first select, the second is filled via jQuery. The form works correctly except for the order the multiselect is filled.

e.g. The "product" select contains:

<select name="product_id" id="products" multiple="multiple">
<option value="#" selected="selected">Please Select</option>
<option value="1">Product 1</option>
<option value="5">Product 2</option>
<option value="4">Product 3</option>
<option value="6">Product 4</option>

(Yes, the values are not the same as the product numbers) When a user selects one of these the jQuery call populates the next dropdown.

<select name="version_id" id="versions" multiple="multiple">
<option value="100">3.0.7</option>
<option value="101">1.0.8</option>
<option value="102">12.0.1</option>

The code fills this select and orders by "value", is there a way to force it to order by the actual content e.g. change it to:

<option value="101">1.0.8</option>
<option value="100">3.0.7</option>
<option value="102">12.0.1</option>

The success code in my call is:

success: function(versions) 
var opt = $('<option />'); 


图片转代码服务由CSDN问答提供 功能建议

我为我的网站构建了一个动态填充的多选表单。 当用户在第一个选择中选择一个或多个项目时,第二个项目通过jQuery填充。 表单正常工作,但多重选择的顺序除外。

e.g。 “product”选项包含:

 &lt; select name =“product_id”id =“products”multiple =“multiple”&gt; 
&lt; option value =“#”  selected =“selected”&gt;请选择&lt; / option&gt; 
&lt; option value =“1”&gt;产品1&lt; / option&gt; 
&lt; option value =“5”&gt;产品2&lt; / option&gt; 
&lt; 选项值=“4”&gt;产品3&lt; /选项&gt; 
&lt;选项值=“6”&gt;产品4&lt; /选项&gt; 

(是 ,值与产品编号不同) 当用户选择其中一个时,jQuery调用会填充下一个下拉列表。

 &lt; select name =“version_id”id =“versions”multiple =“multiple”&gt; 
&lt; option value =“100”&gt; 3.0.7&lt; / 选项&gt; 
&lt; option value =“101”&gt; 1.0.8&lt; / option&gt; 
&lt; option value =“102”&gt; 12.0.1&lt; / option&gt; 

代码用“值”填充此选择和订单,有没有办法强制它按实际内容排序,例如 将其更改为:

 &lt; option value =“101”&gt; 1.0.8&lt; / option&gt; 
&lt; option value =“100”&gt; 3.0.7&lt;  / option&gt; 
&lt; option value =“102”&gt; 12.0.1&lt; / option&gt; 


 $ .each(versions,function(id,version)
var opt = $('&lt; option /&gt;'  ); 


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhi2332 2012-04-16 10:04

    Thanks to @deifwud for pointing me in the right direction. In the end I added the following directly after my success function. It re-orders the results sorting by the text instead of the value.

    $("#versions").html($("#versions option").sort(function (a, b) {
    return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    点赞 评论
  • doupu1949 2012-04-10 09:57

    you must be doing a ajax call to a file where a sql query is executed to dynamically generate the next dropdown box.... if this is the case then you need to place the keyword "ASC" at the last of select query which is yielding your "id" in value attribute of <option value"">

    点赞 评论

相关推荐 更多相似问题