让每个功能结果显示分开

Getting each function results to show separated

我有一个 div,它作为选择的着陆点,有点像自定义选择输入。如果单击 "Make Selection",下面会出现一个菜单。然后您的选择(一项或多项)将出现在 div.

我有两个问题无法解决。

  1. 如何让每个选择显示在 .drop-item-selected 中,以便选择看起来像在 dividual 框中。 (现在 div 包含所有选择,如果选择了多个)。

  2. 如何从下拉列表中删除 .drop-item-inputs。注释掉的 javascript 是我尝试过的,但只是删除了整个列表。

有人有什么想法吗?

对于 #1,我希望盒子分别包裹在 div 中,因此它们看起来像这样:

Here is a jsfiddle.

$('#proposal-type').click(function() {
  $('#proposal-type-drop').addClass('active');
});
$('.drop-item-input').on('change', function() {
  var proposalVal = "";
  $('.drop-item-input').each(function() {
    if ($(this).is(":checked")) {
      proposalVal += $(this).val();
      //$(this).fadeOut();
    };
    /*if ($('.drop-item-input').is(':checked')) {
     $('.drop-item').fadeOut();
    }*/
    $('#proposal-type').val(proposalVal).html("<div class='drop-item-selected'>" + proposalVal + "</div>");
    $('#proposal-type-drop').removeClass('active');
  });
});
#proposal-type-drop {
  width: 45%;
  display: none;
  position: absolute;
}

#proposal-type-drop.active {
  background: rgba(0, 0, 0, 1);
  display: block;
  height: auto;
  z-index: 1;
}

[contentEditable=true]:empty:not(:focus):before {
  content: attr(data-text)
}

.drop-item {
  color: #FFF;
  font-size: .9rem;
  padding: 5px;
  background: #000;
  display: block;
  width: 100%;
  cursor: pointer;
}

.drop-item-input {
  display: none;
}

.drop-item-selected {
  background: blue;
  padding: 5px;
  font-size: .9rem;
  width: auto;
  display: inline-block;
  margin: 0 3px;
}

.proposal-text {
  width: 95%;
  display: block;
  height: 6em;
  margin: 1.5% 2% 2.5% 2%;
  !important
}

#proposal-check {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="proposal-type" contentEditable=true name="proposal_type" class="proposal-input" data-text="Make Selection"></div>
<div id="proposal-type-drop">
  <label class="drop-item">A<input type="checkbox" class="drop-item-input" value="A"></label>
  <label class="drop-item">B<input type="checkbox" class="drop-item-input" value="B"></label>
  <label class="drop-item">C<input type="checkbox" class="drop-item-input" value="C"></label>
</div>

对于您要求的,只需执行:

$('#proposal-type').click(function() {
    $('#proposal-type-drop').addClass('active');
});
$('.drop-item-input').on('change', function() {
    var proposalVal = "";
    var proposalHtml = "";
    $('.drop-item-input').each(function() {
        if ($(this).is(":checked")) {
            proposalVal += $(this).val();
            proposalHtml += '<span class="drop-item-selected">' + $(this).val() + '</span>';
            $(this).closest('label').fadeOut();
        };
        $('#proposal-type').val(proposalVal).html(proposalHtml);
        $('#proposal-type-drop').removeClass('active');
    });
});
#proposal-type-drop {
    width: 45%;
    display: none;
    position: absolute;
}

#proposal-type-drop.active {
    background: rgba(0, 0, 0, 1);
    display: block;
    height: auto;
    z-index: 1;
}

[contentEditable=true]:empty:not(:focus):before {
    content: attr(data-text)
}

.drop-item {
    color: #FFF;
    font-size: .9rem;
    padding: 5px;
    background: #000;
    display: block;
    width: 100%;
    cursor: pointer;
}

.drop-item-input {
    display: none;
}

.drop-item-selected {
    display: inline-block;
    background: blue;
    padding: 5px;
    font-size: .9rem;
    width: auto;
    display: inline-block;
    margin: 3px;
}

.proposal-text {
    width: 95%;
    display: block;
    height: 6em;
    margin: 1.5% 2% 2.5% 2%;
    !important
}

#proposal-check {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="proposal-type" contentEditable=true name="proposal_type" class="proposal-input" data-text="Make Selection"></div>
<div id="proposal-type-drop">
    <label class="drop-item">A
        <input type="checkbox" class="drop-item-input" value="A">
    </label>
    <label class="drop-item">B
        <input type="checkbox" class="drop-item-input" value="B">
    </label>
    <label class="drop-item">C
        <input type="checkbox" class="drop-item-input" value="C">
    </label>
</div>

也在 JSFiddle.

   

 

    $('#proposal-type').click(function() {
          $('#proposal-type-drop').addClass('active');
        });
        $('.drop-item-input').on('change', function() {
          var proposalVal = "";
    
            if ($(this).is(":checked")) {
              proposalVal += $(this).val();
    $('#proposal-type').append("<div class='drop-item-selected'>" + proposalVal + "</div>&nbsp;");
            $('#proposal-type-drop').removeClass('active');
 $(this).closest('label').fadeOut();
            }
            /*if ($('.drop-item-input').is(':checked')) {
             $('.drop-item').fadeOut();
            }*/
        
         
        });
#proposal-type-drop {
  width: 45%;
  display: none;
  position: absolute;
}

#proposal-type-drop.active {
  background: rgba(0, 0, 0, 1);
  display: block;
  height: auto;
  z-index: 1;
}

[contentEditable=true]:empty:not(:focus):before {
  content: attr(data-text)
}

.drop-item {
  color: #FFF;
  font-size: .9rem;
  padding: 5px;
  background: #000;
  display: block;
  width: 100%;
  cursor: pointer;
}

.drop-item-input {
  display: none;
}

.drop-item-selected {
  background: blue;
  padding: 5px;
  font-size: .9rem;
  width: auto;
  display: inline-block;
  margin: 0 3px;
}

.proposal-text {
  width: 95%;
  display: block;
  height: 6em;
  margin: 1.5% 2% 2.5% 2%;
  !important
}

#proposal-check {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="proposal-type" contentEditable=true name="proposal_type" class="proposal-input" data-text="Make Selection"></div>
<div id="proposal-type-drop">
  <label class="drop-item">A<input type="checkbox" class="drop-item-input" value="A"></label>
  <label class="drop-item">B<input type="checkbox" class="drop-item-input" value="B"></label>
  <label class="drop-item">C<input type="checkbox" class="drop-item-input" value="C"></label>
</div>