Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

html - JavaScript dependent drop down list

How do you make a dependent drop down list with JavaScript?

I need first category list to trigger other 2 (or more) categories to show up with lists that depends on what was your selection on first category.

Is that possible?

Here's my code :

<code> 

function dropdownlist(listindex)
 {

document.formname.category2.options.length = 0;
 switch (listindex)
 {

 case "Home Ware" :
 document.formname.category2.options[0]=new Option("Select Category2","");
 document.formname.category2.options[1]=new Option("Air-Conditioners/Coolers","Air-Conditioners/Coolers");
 document.formname.category2.options[2]=new Option("Audio/Video","Audio/Video");
 document.formname.category2.options[3]=new Option("Beddings","Beddings");
 document.formname.category2.options[4]=new Option("Camera","Camera");
 document.formname.category2.options[5]=new Option("Cell Phones","Cell Phones");

 break;

 case "Education" :
 document.formname.category2.options[0]=new Option("Select Category2","");
 document.formname.category2.options[1]=new Option("Colleges","Colleges");
 document.formname.category2.options[2]=new Option("Institutes","Institutes");
 document.formname.category2.options[3]=new Option("Schools","Schools");
 document.formname.category2.options[4]=new Option("Tuitions","Tuitions");
 document.formname.category2.options[5]=new Option("Universities","Universities");

 break;

 case "Books" :
 document.formname.category2.options[0]=new Option("Select Category2","");
 document.formname.category2.options[1]=new Option("College Books","College Books");
 document.formname.category2.options[2]=new Option("Engineering","Engineering");
 document.formname.category2.options[3]=new Option("Magazines","Magazines");
 document.formname.category2.options[4]=new Option("Medicine","Medicine");
 document.formname.category2.options[5]=new Option("References","References");

 break;

 }
 return true;
 }
</code>

Html :

    <form id="formname" name="formname" method="post" action="submitform.asp" >
 <table width="50%" border="0" cellspacing="0" cellpadding="5">
 <tr>
 <td width="41%" align="right" valign="middle">Category1 :</td>
 <td width="59%" align="left" valign="middle"><select name="category1" id="category1" onchange="javascript: dropdownlist(this.options[this.selectedIndex].value);">
 <option value="">Select Category1</option>
 <option value="Home Ware">Home Ware</option>
 <option value="Education">Education</option>
 <option value="Books">Books</option>
 </select></td>
 </tr>
 <tr>
 <td align="right" valign="middle">Category2 :
 </td>
 <td align="left" valign="middle"><script type="text/javascript" language="JavaScript">
 document.write('<select name="category2"><option value="">Select Category2</option></select>')
 </script>
 <noscript><select name="category2" id="category2" >
 <option value="">Select Category2</option>
 </select>
 </noscript></td>
 </tr>
 </table>

</form>

I believe it is not possible to create a third list that would be triggered by choosing "Home ware" and have different selections?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE 03/2018

I review this solution because an OP (thx @stavasknall) told me that my solution doesn't work in Safari.

The issue:

In some browser (like Safari) the <option> inside a <select> can't be hided via CSS, if you set (in css or via Jquery) style="display:none" Safari simply ignores it.

My new solution

To target the OP goal is necessary to manipulate the DOM to change the <option> inside e selection, to do this there are a lot of solution i follow this one:

HTML

<form id="formname" name="formname" method="post" action="submitform.asp" >
<table width="50%" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td width="41%" align="right" valign="middle">Category1 :</td>
    <td width="59%" align="left" valign="middle">
      <select name="category1" id="category1">
        <option value="">Select Category1</option>
        <option value="home_ware">Home Ware</option>
        <option value="education">Education</option>
        <option value="books">Books</option>
      </select>
    </td>
  </tr>
  <tr>
    <td align="right" valign="middle">Category2 :</td>
    <td align="left" valign="middle">
      <select disabled="disabled" class="subcat" id="category2" name="category2">
        <option value>Select Category2</option>
        <!-- Home Ware -->
        <optgroup data-rel="home_ware">
          <option value="air-conditioners_coolers">Air-Conditioners/Coolers</option>
          <option value="audio-video">Audio/Video</option>
          <option value="beddings">Beddings</option>
          <option value="camera">Camera</option>
          <option value="cell-phones">Cell Phones</option>
        </optgroup>
        <!-- Education -->
        <optgroup data-rel="education">
          <option value="Colleges">Colleges</option>
          <option value="Institutes">Institutes</option>
          <option value="Schools">Schools</option>
          <option value="Tuitions">Tuitions</option>
          <option value="Universities">Universities</option>
        </optgroup>
        <!-- Books -->
        <optgroup data-rel="books">
          <option value="College Books">College Books</option>
          <option value="Engineering">Engineering</option>
          <option value="Magazines">Magazines</option>
          <option value="Medicine">Medicine</option>
          <option value="References">References</option>
        </optgroup>
      </select>
    </td>
  </tr>
  <tr>
    <td align="right" valign="middle">Category3 :</td>
    <td align="left" valign="middle">
      <select disabled="disabled" class="subcat" id="category3" name="category3">
        <option value>Select Category3</option>
        <!-- Home Ware -->
        <optgroup data-rel="home_ware">
          <option value="foo1">category3 home ware 1</option>
          <option value="foo2">category3 home ware 2</option>
        </optgroup>
        <!-- Education -->
        <optgroup data-rel="education">
          <option value="foo3">category3 Education 1</option>
          <option value="foo4">category3 Education 2</option>
        </optgroup>
        <!-- Books -->
        <optgroup data-rel="books">
          <option value="foo5">category3 Books 1</option>
          <option value="foo6">category3 Books 2</option>
        </optgroup>
      </select>
    </td>
  </tr>
</table>

Javascript

var $cat = $("#category1"),
    $subcat = $(".subcat");

var optgroups = {};

$subcat.each(function(i,v){
  var $e = $(v);
  var _id = $e.attr("id");
  optgroups[_id] = {};
  $e.find("optgroup").each(function(){
    var _r = $(this).data("rel");
    $(this).find("option").addClass("is-dyn");
    optgroups[_id][_r] = $(this).html();
  });
});
$subcat.find("optgroup").remove();

var _lastRel;
$cat.on("change",function(){
    var _rel = $(this).val();
    if(_lastRel === _rel) return true;
    _lastRel = _rel;
    $subcat.find("option").attr("style","");
    $subcat.val("");
    $subcat.find(".is-dyn").remove();
    if(!_rel) return $subcat.prop("disabled",true);
    $subcat.each(function(){
      var $el = $(this);
      var _id = $el.attr("id");
      $el.append(optgroups[_id][_rel]);
    });
    $subcat.prop("disabled",false);
});

In this the script save the s html inside a Javascript object (based on <select> ID and <optgroup> data-rel) and remove them from the DOM. With this solution when a <select> change the script find the related option and print it in the relative <select>.

Like in other solution also in this one you must include jQuery and you have to wrap the Javascript inside DOMready listener or at the end of the <body>.

Working example:

http://jsfiddle.net/v917ycp6/595


ORIGINAL SOLUTION

(non CROSS BROWSER - not work with webkit)

I change your HTML, JS, CSS to prevent the excessive use of Javascript.

  1. First: Consider to use jQuery (a simple JS library that help you to create complex thing)
  2. Not create/remvoe DOM element every time you need to change your HTML - instead HIDE this element and show all you nedd.

HTML

<form id="formname" name="formname" method="post" action="submitform.asp" >
  <table width="50%" border="0" cellspacing="0" cellpadding="5">
    <tr>
      <td width="41%" align="right" valign="middle">Category1 :</td>
      <td width="59%" align="left" valign="middle">
        <select name="category1" id="category1">
          <option value="">Select Category1</option>
          <option value="home_ware">Home Ware</option>
          <option value="education">Education</option>
          <option value="books">Books</option>
        </select>
      </td>
    </tr>
    <tr>
      <td align="right" valign="middle">Category2 :</td>
      <td align="left" valign="middle">
        <select disabled="disabled" id="category2" name="category2">
          <option class="label" value>Select Category2</option>
          <!-- Home Ware -->
          <option rel="home_ware" value="air-conditioners_coolers">Air-Conditioners/Coolers</option>
          <option rel="home_ware" value="audio-video">Audio/Video</option>
          <option rel="home_ware" value="beddings">Beddings</option>
          <option rel="home_ware" value="camera">Camera</option>
          <option rel="home_ware" value="cell-phones">Cell Phones</option>
          <!-- Education -->
          <option rel="education" value="Colleges">Colleges</option>
          <option rel="education" value="Institutes">Institutes</option>
          <option rel="education" value="Schools">Schools</option>
          <option rel="education" value="Tuitions">Tuitions</option>
          <option rel="education" value="Universities">Universities</option>
          <!-- Books -->
          <option rel="books" value="College Books">College Books</option>
          <option rel="books" value="Engineering">Engineering</option>
          <option rel="books" value="Magazines">Magazines</option>
          <option rel="books" value="Medicine">Medicine</option>
          <option rel="books" value="References">References</option>
        </select>
      </td>
    </tr>
  </table>
</form>

take a look: I use rel attribute to link category and sub category (to lowercase and without space or special Char):

<option rel="home_ware" value="">Select Category1</option>

CSS

#category2 option{
    display:none;
}

#category2 option.label{
    display:block;
}

this CSS hide the subcategory options (not label) that show only if the main category is seleceted.

JS (jQuery)

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script
<script>
$(function(){

//fisrt I store in 2 var the reference of two <select>

var $cat = $("#category1"),
$subcat = $("#category2");

//this is the same thing if you write in your HTML onChange="" in first <select>

$cat.on("change",function(){

//store the value of first select every time it change
var _rel = $(this).val();

//clean the second select (value and option active) to prevent bad link (cat1 with subcat of cat2)
$subcat.find("option").attr("style","");
$subcat.val("");

//if no option is selected i disable the second select
if(!_rel) return $subcat.prop("disabled",true);

//if a option si selected i show the option linked by rel attr
$subcat.find("[rel="+_rel+"]").show();
$subcat.prop("disabled",false);
});

});
</script>

I use jQuery because is simple, clean, and you write less code. Take a look to jQuery's Documentation: http://api.jquery.com/. If you never use it, you should.

This resurce is needed

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>

Working example:

http://jsfiddle.net/v917ycp6/5/

Edit

If you need to enable multiple <select> after you check one option of category1 you simply change the selector of the variable $subcat to select more than one <select>.

HTML

add class .subcat to all <select> you need to enable when select category1:

<select disabled="disabled" id="category2" name="category2">

becomes:

<select disabled="disabled" id="category2" class="subcat" name="category2">

Now, add the new <select> to your code:

<select disabled="disabled" class="subcat" id="category3" name="category3">
<option value>Select Category3</option>
<!-- Home Ware -->
<option rel="home_ware" value="foo1">category3 home ware 1</option>
    <option rel="home_ware" value="foo2">category3 home ware 2</option>
<!-- Education -->
<option rel="Education" value="foo3">category3 Education 1</option>
<option rel="Education" value="foo4">category3 Education 2</option>

<!-- Books -->
<option rel="Books" value="foo5">category3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...