最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

firefox - Adding elements to dropdownlist through javascript - Stack Overflow

programmeradmin0浏览0评论

Am unable to add elements to a dropdown list via Javascript.

The below piece of code works in IE and Chrome, but not in firefox.

ddlId.add(new Option("",0));

In firefox, I keep getting an 'Not enough arguments' exception. Any idea on how to resolve it? Thanks

Am unable to add elements to a dropdown list via Javascript.

The below piece of code works in IE and Chrome, but not in firefox.

ddlId.add(new Option("",0));

In firefox, I keep getting an 'Not enough arguments' exception. Any idea on how to resolve it? Thanks

Share Improve this question edited Mar 25, 2014 at 16:29 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Jun 8, 2010 at 16:32 user355562user355562 3,3733 gold badges22 silver badges18 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4
try {
    ddlId.add(new Option("",0), null);  // standards pliant; doesn't work in IE
} catch(ex) {
    ddlId.add(new Option("",0));    // IE only
}

Hm. The idea is, roughly, to go to the Mozilla Developer Center page for select.add() and have a look at the method signature ;-)

Syntax

select.add(newOption, existingOption);

Parameters

newOption
An HTMLOptionElement to add to the options collection.

existingOption
An existing HTMLOptionElement within the collection used as a reference point for inserting the new element; the new element being inserted before the referenced element in the collection. If this parameter is null, the new element is appended to the end of the collection.

var opt = document.createElement("option");
  var ddlPopulate=document.getElementById("<%=ddlPopulate.ClientId %>");
opt.text="firstElement";
  opt.value="1";
  ddlPopulate.options.add (opt);

The select element has as its children an options array. You add or remove options as you would using standard array methods.

发布评论

评论列表(0)

  1. 暂无评论