【问题标题】:Getting each function results to show separated让每个函数结果分开显示
【发布时间】:2017-05-01 22:43:29
【问题描述】:

我有一个div,它用作选择的着陆场所,类似于自定义选择输入。如果单击“进行选择”,则会在下方显示一个菜单。然后您的选择(一个或多个项目)将出现在该 div 中。

我有两个我无法弄清楚的问题。

  1. 如何让每个选择显示在 .drop-item-selected 中,以便选择看起来像单独的框。 (现在 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>

【问题讨论】:

  • 如果你想在 contentEditable 中创建标签,那么你需要更多的逻辑。例如,如果有人键入“A”,那么您需要将其包装在 span 中并在下拉列表中检查/隐藏它,对吗?另外,您是否允许输入“F”,因为它不在列表中?
  • @skobaljic 标签与 contentEditable 无关。我正在制作的“标签”是单独的输入,点击时会出现。
  • 我仍然不清楚目标是什么,您需要什么功能。您在这个问题中提出的问题很简单,但我怀疑答案会对您有所帮助,因为我认为还有更多工作要做。
  • @skobaljic 我添加到我的问题中以澄清。
  • 好的,对于 1 只需执行 this way,创建要附加的 html。

标签: javascript jquery function each


【解决方案1】:

按照你的要求去做:

$('#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

【讨论】:

  • 谢谢!如果从列表中删除标签,是否有办法让它回到下拉列表中?
  • 这就是我所说的功能。当然有办法,但你没有要求。
  • 我该怎么做?
  • (抱歉跑题了)...但是所有这些东西都太用户不友好...我什至没有注意到选择一个选项后还有更多选择-添加....
  • 我永远不会从头开始,除非我必须这样做。去谷歌搜索jquery tag dropdown,你会发现很多插件,比如this one。因为我现在会有更多问题:如何从上面的列表中删除?使用键盘删除或单击...等等。然后,我提到了另一件事:如果用户在可编辑的内容中键入“F”或“B”怎么办...您的脚本将增加三倍。
【解决方案2】:

   

 

    $('#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>

【讨论】:

  • 抱歉我提交的太匆忙了。立即检查。
猜你喜欢
  • 1970-01-01
  • 2016-12-05
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
相关资源
最近更新 更多