用文本值对填充选择列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用文本值对填充选择列表相关的知识,希望对你有一定的参考价值。

This snippet describes how to fill select html element with Text, Value pairs
where 'Text' is displayed to the user and 'Value' is actual value to be processed.
  1. function FillSelectHtmlElement(ctrlId,result) {
  2. $(ctrlId).get(0).options.length = 0;
  3. $(ctrlId).get(0).options[0] = new Option("-- Make your selection --", "-1");
  4. //$('<option/>').appendTo(ctrlId); // uncomment this line if you wish an empty select option
  5.  
  6. $.each(result, function (index, item) {
  7. $(ctrlId).get(0).options[$(ctrlId).get(0).options.length] = new Option(item.Text, item.Value);
  8. });
  9. }
  10.  
  11. Call this function like this:
  12. var ddl = $("#myDropDownList");
  13. var someTestValues = [{ "Text": "Selection 1", "Value": "Value1" }, { "Text": "Selection 2", "Value": "Value2"}];
  14. FillSelectHtmlElement(ddl,someTestValues );

以上是关于用文本值对填充选择列表的主要内容,如果未能解决你的问题,请参考以下文章