Dynamic Dropdown using semantic ui

六眼飞鱼酱① 提交于 2019-12-23 16:20:52

问题


Got a problem with semantic-ui dropdown. I've been using Semantic-Ui, and wanted to change the dropdown item dynamically. That is, when i choose the value from the first dropdown, the second dropdown's item is not getting reflected.

Here is the code snippet:

$(document).ready(function() {
  $("#programmetype").dropdown({
    onChange: function() {

      $('#servicetype').html(
        '<div class="ui selection dropdown select- 
        language ">'+'<div class="
        text ">Choose..</div>'+'<i class="
        dropdown icon "></i>'+' <div class="
        menu ">'+


        '<div class="item" data-value="acp">ACP</div>' +
        '<div class="item" data- 
        value = "art" > ART < /div>'+'</div > '+' < /div>'
      );
      $('#servicetype').dropdown();
    }

  });
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>


<div id="programmetype" class="ui selection dropdown select- 
        language">
  <input type="hidden" name="programmetype">
  <div class="text">Choose..</div>
  <i class="dropdown icon"></i>
  <div class="menu">
    <div class="item" data-value="val1">Car</div>
    <div class="item" data-value="val2">Tank</div>
    <div class="item" data-value="val3">Plane</div>
  </div>
</div>

<div id="servicetype"></div>

回答1:


Syntax Errors

You are making line breaks inside your strings, break the lines properly with a + operator and your script won't find a syntax error, thus your code should run.

Second Dropdown

About the second dropdown. you're missing something. Look at the first dropdown DOM structure. the parent element #programmetype has the classes ui selection dropdown select-language for the CSS to get applied.

But for your #servicetype, you don't have those classes and you never added them, so in the onChange of your first dropdown, add these classes, (check the script)

Also, you need a hidden input to hold the data which you missed. But it's there for the first dropdown. Check the snippet, I've added them.

Hope this helps.

Check the snippet bellow:

$(document).ready(function() {
  $("#programmetype").dropdown({
    onChange: function() {

      $('#servicetype').addClass('ui selection dropdown select-language'); // add these classes for the UI.
      $('#servicetype').html(
        '<input type="hidden" name="servicetype">' // you need a hidden input and the rest is fine
        +'<div class="text">Choose..</div>'
        +'<i class="dropdown icon "></i>'
        +'<div class="menu">'
        +'<div class="item" data-value="acp">ACP</div>'
        +'<div class="item" data-value="art"> ART</div>'
        +'</div>'
        +'</div>'
      );
      $('#servicetype').dropdown();
    }

  });
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>


<div id="programmetype" class="ui selection dropdown select-language">
  <input type="hidden" name="programmetype">
  <div class="text">Choose..</div>
  <i class="dropdown icon"></i>
  <div class="menu">
    <div class="item" data-value="val1">Car</div>
    <div class="item" data-value="val2">Tank</div>
    <div class="item" data-value="val3">Plane</div>
  </div>
</div>

<div id="servicetype"></div>


来源:https://stackoverflow.com/questions/56341481/dynamic-dropdown-using-semantic-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!