laravel PHP space 个字符中的下拉列表不起作用

Dropdown list in laravel PHP space characters not working

我有两个下拉菜单,当我点击第一个里面的项目时,第二个会自动更新,我用 JQuery 来做这个。它工作正常,但前提是第一个列表中的项目内没有 space 个字符,例如,如果我有 "Some item" 作为项目,它什么都不做,但对于 "item"它将更新另一个列表。这是 Jquery 代码和 Laravel PHP 代码。你能告诉我为什么第一个下拉列表中的 spaces 有问题吗?

  <select name="podrucje" id="select1" style='width: 150px;'>                     
          @foreach( $projects_all as $project )
           <? if ($project->name != null)  { ?>

      <option value="{{ $project->podrucje }}">{{ $project->name}}</option>

               <? }  ?>
      @endforeach
      </select>

这是 Jquery 第一个列表的项目更改脚本:

     $("#select1").change(function () {
    if ($(this).data('options') == undefined) {
        /*Taking an array of all options-2 and kind of embedding it on the select1*/
        $(this).data('options', $('#select2 option').clone());
    }
    var id = $(this).val();
    var options = $(this).data('options').filter('[value=' + id + ']');
    $('#select2').html(options);



    });

如果您在过滤器中的值周围加上双引号,似乎可以工作。

所以你的 js 的第 7 行会变成

var options = $(this).data('options').filter('[value="' + id + '"]');

查看此工作示例(下拉列表中的最后一个值的值为 space)

https://jsfiddle.net/5mb7oot4/1/

您需要将过滤值括在引号中。

var options = $(this).data('options').filter('[value="' + id + '"]');