class未应用于动态添加的下拉列表

时间:2017-09-20 10:30:58

标签: javascript css

您好我在按钮点击时添加下拉列表但样式未应用于该下拉列表

$(function() {
  $('.DropdownList').multiselect({
    includeSelectAllOption: true
  });

  $('#btnSelected').click(function() {
    var selected = $("#EmpList option:selected");
    var message = "";
    selected.each(function() {
      message += $(this).text() + " " + $(this).val() + "\n";
    });
    alert(message);
  });

  $('#btn').click(function() {
    $('#drpDownContainter').append("<select id=EmpList class=DropdownList multiple=multiple><option value=1>Navdeep-Asp.net</option><option value=2>Pankaj-C#</option><option value=3>Bikesh-Asp.Net</option><option value=4>Pritam-Salesforce</option><option value=5>Payal-Salesforce</option><option value=6>Sujata-SSRS</option><option value=7>Harikant-UI</option></select>");

  });

});
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>

<div>

  <input type="button" id="btn" value="Add" />

  <table>
    <tr>
      <td id="drpDownContainter">
        <select id="EmpList" class="DropdownList" multiple="multiple">
                    <option value="1">Navdeep-Asp.net</option>
                    <option value="2">Pankaj-C#</option>
                    <option value="3">Bikesh-Asp.Net</option>
                    <option value="4">Pritam-Salesforce</option>
                    <option value="5">Payal-Salesforce</option>
                    <option value="6">Sujata-SSRS</option>
                    <option value="7">Harikant-UI</option>

                </select>

        <select id="EmpList" class="DropdownList" multiple="multiple">
                    <option value="1">Navdeep-Asp.net</option>
                    <option value="2">Pankaj-C#</option>
                    <option value="3">Bikesh-Asp.Net</option>
                    <option value="4">Pritam-Salesforce</option>
                    <option value="5">Payal-Salesforce</option>
                    <option value="6">Sujata-SSRS</option>
                    <option value="7">Harikant-UI</option>

                </select>
      </td>
      <td><input type="button" id="btnSelected" value="Get Selected" /></td>
    </tr>
  </table>


</div>

以下是输出 output image

2 个答案:

答案 0 :(得分:0)

您需要将.multiselect应用于新添加的下拉列表,因为当您最初在页面加载时应用它时,页面上没有新的下拉列表。

$('#btn').click(function () {

    var dropdown = $("<select class=DropdownList multiple=multiple><option value=1>Navdeep-Asp.net</option><option value=2>Pankaj-C#</option><option value=3>Bikesh-Asp.Net</option><option value=4>Pritam-Salesforce</option><option value=5>Payal-Salesforce</option><option value=6>Sujata-SSRS</option><option value=7>Harikant-UI</option></select>");
    $('#drpDownContainter').append(dropdown);
    dropdown.multiselect({
        includeSelectAllOption: true
    });

});

附注:ID应该是唯一的,并且您正在重复使用相同的ID。

答案 1 :(得分:0)

这可能会有所帮助

$('#btn').click(function () {
            $('#drpDownContainter').append("<select id=EmpList class=DropdownList multiple=multiple><option value=1>Navdeep-Asp.net</option><option value=2>Pankaj-C#</option><option value=3>Bikesh-Asp.Net</option><option value=4>Pritam-Salesforce</option><option value=5>Payal-Salesforce</option><option value=6>Sujata-SSRS</option><option value=7>Harikant-UI</option></select>");

          $('.DropdownList').multiselect({
              includeSelectAllOption: true
          });
        });