如何使用javascript创建子类别?

时间:2018-02-10 14:45:40

标签: javascript jquery html html5

大家好,我需要帮助。

我需要先选择必须选择模型类型,必须要求吗?

如何使id =“subcategory”成为必需?

id =“subcategory”所需的

为什么不起作用?

<select id="category" required>
    <option value="">select model type</option>
    <option class="Volvo" value="1">Volvo</option>
    <option class="Saab" value="2">Saab</option>
    <option class="Opel" value="3">Opel</option>
    <option class="Audi" value="4">Audi</option>
</select> 
 <select id="subcategory">
  <optgroup class="Volvo">
    <option value="">select model type</option>
    <option value="44">XC60</option>
    <option value="55">XC90</option>
  </optgroup>
  <optgroup class="Saab">
    <option value="">select model type</option>
    <option value="66">Saab 9XX</option>
    <option value="77">Saab Aero-X</option>
  </optgroup>
  <optgroup class="Opel">
    <option value="">select model type</option>
    <option value="88">Corsa A</option>
    <option value="99">Corsa B</option>
  </optgroup>
  <optgroup class="Audi">
    <option value="">select model type</option>
    <option value="616">Audi A4</option>
    <option value="717">Audi A8</option>
  </optgroup>
</select> 




  <script>
$('#subcategory').find('optgroup').hide(); // initialize
$('#category').change(function() {
 var $cat = $(this).find('option:selected');
 var $subCat = $('#subcategory').find('.' + $cat.attr('class'));
 $('#subcategory').find('optgroup').not("'"+ '.' + $cat.attr('class') + "'").hide(); // hide other optgroup
 $subCat.show();
 $subCat.find('option').first().attr('selected', 'selected');
});
    </script>

1 个答案:

答案 0 :(得分:0)

以下是我稍微更新第二个选项以在select mode type之外设置optgroup的想法。您将看到在选择这两个值之前,您无法点击发送。

您可以参考此链接了解我的更改:

https://stackoverflow.com/a/6048891/8620333

&#13;
&#13;
$('#subcategory').find('optgroup').hide(); // initialize
$('#category').change(function() {
  var $cat = $(this).find('option:selected');
  var $subCat = $('#subcategory').find('.' + $cat.attr('class'));
  $('#subcategory').find('optgroup').not("." + $cat.attr('class')).hide(); 
  $subCat.show();
  $('#subcategory option').removeAttr('selected');
  $('#subcategory > option').attr('selected', 'selected');
  //adding this will make it required
  $('#subcategory').attr('required','required')
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select id="category" required>
    <option value="" disabled selected>select model type</option>
    <option class="Volvo" value="1">Volvo</option>
    <option class="Saab" value="2">Saab</option>
    <option class="Opel" value="3">Opel</option>
    <option class="Audi" value="4">Audi</option>
</select>
<select id="subcategory">
  <option value="">select model type</option>
  <optgroup class="Volvo">
    <option value="44">XC60</option>
    <option value="55">XC90</option>
  </optgroup>
  <optgroup class="Saab">
    <option value="66">Saab 9XX</option>
    <option value="77">Saab Aero-X</option>
  </optgroup>
  <optgroup class="Opel">
    <option value="88">Corsa A</option>
    <option value="99">Corsa B</option>
  </optgroup>
  <optgroup class="Audi">
    <option value="616">Audi A4</option>
    <option value="717">Audi A8</option>
  </optgroup>
</select>
<input type="submit" value="send">
</form>
&#13;
&#13;
&#13;