【问题标题】:How to add a list with results selected from input?如何添加从输入中选择结果的列表?
【发布时间】:2020-02-05 12:34:00
【问题描述】:

我有一个带有列表和一些选项的模式。这些是从数据库中获取的。我需要用我在该模式中选择的内容制作一个列表/输入。我能做到吗?

这是我的清单:

    <section> 
             <input type="text" class="form-control" id="populerNameKeyOrg" placeholder="Search for contacts" style="margin-top: 20px; margin-bottom: 20px; width: 38%">
              <label class="select">
                 @if(isset($replyMessage))
                   @foreach($users->contact as $user)
                         <ul id="destPopulerOrg">
                     <li class="countryOrg"><input name="target_id[]" type="checkbox" value="{{ $user->id }}" <?php if(isset($replyMessage)){ if($replyMessage == $user->id) { echo "selected"; } } ?>><span>{{ $user->username }}</span></li>
                             <ul>
                         @endforeach
                 @endif
               </label>
            </section>

每个输入都有一个自己的 ID(用户 ID),就像这里的 &lt;input name="target_id[]" type="checkbox" value="548"&gt;。因此,我需要使用在我的模式中选择的值(用户名)来制作类似于 Input 的东西。

【问题讨论】:

    标签: php html database laravel


    【解决方案1】:

    试试这个

     <section> 
       <input type="text" class="form-control" id="populerNameKeyOrg" placeholder="Search for contacts" style="margin-top: 20px; margin-bottom: 20px; width: 38%">
        <label class="select">
           @if(isset($replyMessage))
            @foreach($users->contact as $user)
              <ul id="destPopulerOrg">
               <li class="countryOrg"><input name="target_id[]" type="checkbox" value="{{ $user->id }}" <?php if(isset($replyMessage)){ if($replyMessage == $user->id) { echo "checked"; } } ?>><span>{{ $user->username }}</span></li>
    
              <ul>
             @endforeach
            @endif
        </label>
    </section>
    

    对了,一定要把selected换成checked

    【讨论】:

    • 谢谢,但似乎不起作用:( imgur.com/a/UjJFVmr
    • 哦,那个输入是用来搜索的。我需要为此做一个单独的输入:) 所以输入是用于搜索哪​​个有效。我需要进行新的输入。
    • 回到你的代码并尝试写checked而不是selected@Andrew
    【解决方案2】:

    正确的解决方案在这里:

    我的李:

    <li class="country">  <input class="my_div" id="{{$user->username}}" name="target_id[]" type="checkbox" value="{{ $user->id }}" <?php if(isset($replyMessage)){ if($replyMessage == $user->id) { echo "selected"; } } ?>><span style="padding: 5px" class="name">{{ $user->username }}</span></li>
    

    结果分类

    Checked users: <div id="results"></div>
    

    脚本

    <script>
    $(document).ready(displayCheckbox);
    
    function displayCheckbox() {
    
        var checkboxes = $(".my_div");
        var results = $("#results");
    
        $.each(checkboxes, function() {
          $(this).change(printChecked);
        })
    
        function printChecked() {
            var checkedIds = [];
    
            checkboxes.each(function() {
                if($(this).is(':checked')) {
                    checkedIds.push($(this).attr('id'));
                }
            });
    
            console.log(results);
            results.text(checkedIds);
        }
    }
    
    
    </script>
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多