开发者

JavaScript not working in Internet Explorer

I am having this dynamic dropdown code. Here i want to load the second dropdown options dynamically by changing the first dropdown options. This works fine in all other browsers except IE. I don't know whats the reason as i'm a starter. could any one explain the reason.

HTML CODE:

<select name="language" id="language" onchange="SetMedia(this)">
<option>Please select a Language</option>
<option>English</option>
<option>Tamil</option>
<option>Telugu</option>
</select>

<select name="media" onChange="processButtonData(this.value)" id="media" disabled="disabled">
<option>select</option>
</select>

JS CODE

<script type="text/javascript">
function SetMedia(objLanguage) {
var objMed开发者_JAVA技巧ia = document.getElementById("media");
objMedia.options.length = 0;
    objMedia.disabled = false;
switch (objLanguage.value) {
case "English":
    objMedia.options.add(new Option("Please Select a Media",""));
    objMedia.options.add(new Option("The Indian Express","http://www.indianexpress.com"));
    objMedia.options.add(new Option("The Hindu","http://www.hindu.com"));
    break;
case "Tamil":
    objMedia.options.add(new Option("Please Select a Media",""));
    objMedia.options.add(new Option("Dinamalar", "http://www.dinamalar.com"));
    objMedia.options.add(new Option("Dinamani", "http://www.dinamani.com"));
    break;
case "Telugu":
    objMedia.options.add(new Option("Please Select a Media",""));
    objMedia.options.add(new Option("Google","http://www.google.com"));
    objMedia.options.add(new Option("Google2","http://www.google.com"));
    break;
default:
    objMedia.options.add(new Option("select"));
    objMedia.disabled = true;
    break;
}
}

</script>


It appears that non-IE browsers (e.g. Chrome) will set the value of an <option> tag implicitly using it's inner-text, but IE does not do this. You have two options:

1) Set the <option> value explicitly

<option value="English">English</option>
<option value="Tamil">Tamil</option>
<option value="Telugu">Telugu</option>

Or,

2) Switch on text

var language = (objLanguage.options[objLanguage.selectedIndex]).text;
switch (language) {
  // ...
}

(Tested in Chrome and IE9.)


Try the alternative using older method to dynamically add option

var option = document.createElement("option");
option.value = "";
option.text = "Please Select a Media";
try {
    objMedia.options.add(option, null);
} catch(exception) {
    objMedia.add(option); // Older IE doesn't support the standard
}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜