javascript - jQuery - Can not select first option on last optgroup -


i have small problem. need select option <select> a, triggered action , select first <option> in <select> b value selected <option> a. works fine, can't select last optgroup , option.

try @ jsfiddle

this bug seems on firefox (linux, win7). google chrome ok. ideas?

code (html):

<select id="charset" name="charset">     <option value=""></option>     <option value="armscii8">armscii-8 armenian</option>     <option value="ascii">us ascii</option>     <option selected="selected" value="utf8">utf-8 unicode</option> </select>  <select id="collation" name="collation">     <optgroup label="armscii8">         <option value="armscii8_bin">armscii8_bin</option>         <option value="armscii8_general_ci">armscii8_general_ci</option>     </optgroup>     <optgroup label="ascii">         <option value="ascii_bin">ascii_bin</option>         <option value="ascii_general_ci">ascii_general_ci</option>     </optgroup>     <optgroup label="utf8">         <option value="utf8_bin" selected="selected">utf8_bin</option>         <option value="utf8_czech_ci">utf8_czech_ci</option>         <option value="utf8_danish_ci">utf8_danish_ci</option>         <option value="utf8_esperanto_ci">utf8_esperanto_ci</option>     </optgroup> </select>` 

code js:

(function($){  function setcollation(charset) {     $('#collation optgroup option').removeattr('selected');     $('#collation optgroup').hide(0);      if (charset && charset != '') {         $("#collation optgroup[label='" + charset + "']").show(0);         $("#collation optgroup[label='" + charset + "'] option:first").attr('selected', 'selected');     } }  $('#charset').change(function() {     setcollation($(this).val()); }); setcollation($('#charset').val());  })(jquery); 

update

i updated jsfiddle removed show/hide functions.

i try reproduce how script works:

  1. when select collation first select box (fe. value dos russin), second select selected need.

  2. i changed first select value one. in moment ok.

  3. ! changed dos russian , in moment second select box not selected in first step.

this strange behavior , can't works in ff.

well can't explain why browser behavior inconsistent can tell changing .prop seemed have solved problem.

 $('#collation optgroup').prop('disabled', true);     $('#collation option').removeattr('selected');      if (charset && charset != '') {         $("#collation optgroup[label='" + charset + "']").prop('disabled',false);         $("#collation optgroup[label='" + charset + "'] > option:eq(0)").prop('selected', true); 

see in action:

http://jsfiddle.net/8paap/1/


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -