2011-12-07 21 views
0

以下代码正在使用Firefox浏览器,但onClick无法使用Chrome和Safari。代码使用Firefox,但不使用Chrome,Safari

下面的代码中是否缺少任何东西?如果有人可以帮助,以便它可以与跨浏览器工作。

谢谢。

<html><body><form method="post"> 
<select> 
<option value="aa">Please select</option> 
<option value="pst" onClick="populate();">Select Existing State</option> 
</select> 

<select name="abc" id="example-select" style="width: 160px;"></select> 

</form> 

<script type="text/javascript" language="javascript"> 
    var example_array = {state1 : '1# First State', state2 : '2# Secondstate', state3 : '3# Third State'}; 

    function populate() { 
     var select = document.getElementById("example-select"); 
     for(index in example_array) { 
      select.options[select.options.length] = new Option(example_array[index], index); 
     } 
    } 
</script> 
</body></html> 
+1

什么东西是不工作? –

+0

onClick不适用于Chrome浏览器和Safari浏览器,但可以与Firefox一起使用 –

+0

http://stackoverflow.com/questions/4340690/javascript-onclick-alert-not-working-in-chrome – Serge

回答

3

很好的onclick seems对WebKit的尝试一些问题,结合populateonchange

+0

onchange不适用于所有。 –

+0

把onChange放在select元素中,像这样:http://jsfiddle.net/6LrSh/1/ – Vigrond

+0

@PujanSrivastava你需要把onchange放在'select'而不是'option'就像这样http://jsfiddle.net/Ka3YK/3/ – Serge

相关问题