开发者

Option list within select hiding and displaying

I have 2 drop downs, I would like to know whether there is a possibility to display and hide some of the options in second drop down based on value selected in first dropdown.

<script language="JavaScript">
 function funchk(){
  document.getElementById('z').style.display="block";

  }

    </script>


    <select name="first" onchange="funchk();">
       <option name="asw" value="a">sda</option>
       <option name="sd" value="sd">ZZ</option>
    <option name="rdf" value="afe">fe</option>
       <option name="bfe" value="bfe">fe3</option>
       </select>         <?php echo "<br/>" ?>
     <select name="second">
      <option name="a" value="a">aa</option>
   开发者_如何转开发      <option name="z" value="a" style="display:none">ZZ</option>
        <option name="r" value="a" style="display:none">aa</option>
         <option name="b" value="b">bb</option>
         </select>

Here i would like to display the 'z' option in 2nd dropdown to be displayed when onchging value of first dropdown.


No, you can't rely on setting the CSS to show/hide <option> elements.

You'll need to remove them, or just simply disable/enable them.

function funchk() {
    document.getElementsByName('z')[0].disabled = false;
}

<select name="second">
    <option name="a" value="a">aa</option>
    <option name="z" value="a" disabled="disabled">ZZ</option>
    <option name="r" value="a" disabled="disabled">aa</option>
    <option name="b" value="b">bb</option>
</select>


you have to do that with javascript

here you can find different examples how to do that.

http://www.satya-weblog.com/2008/08/javascript-remove-delete-select-option.html

http://www.plus2net.com/javascript_tutorial/list-remove.php


Make use of Document Object Model(DOM) along with Javascript Events to manipulate HTML nodes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜