代码之家  ›  专栏  ›  技术社区  ›  detinu20

停止将“无结果”标签添加到输入以及输入是否与标签匹配

  •  0
  • detinu20  · 技术社区  · 6 年前

    1. 我想解决的第一个问题是使noresult标签不可选择,这意味着它不能添加到focus或select输入中。

    2. 我想这样做,如果用户在输入框中输入的内容与标签值匹配,但他们出于某种原因没有选择它,那么当他们按下我表单上的提交按钮时,匹配的标签被选择并因此输入。

    以下是我到目前为止的情况:

    html:

    <div class="search-homepage-input">
        {!! Form::open(['route' => 'search.index', 'method' => 'GET']) !!}
        <div class="col-md-9 col-l">
        {!! Form::text('sl', null, array('class' => 'form-control shop-input', 'maxlength' =>'50', 'placeholder' => 'Search by city. Eg. London, New York, Paris...', 'id' => 'sl')) !!}
        </div>
        {!! Form::hidden('country', null, array('id' => 'country')) !!}
        {!! Form::hidden('city', null, array('id' => 'city')) !!}
        <div class="col-md-3 col-r">
            {!! Form::submit('Find Shops', array('class' => 'btn btn-homepage-search'))  !!}
        </div>
        {!! Form::close() !!}
    </div>
    

       public function autoComplete(Request $request){
    
            $query = $request->term;
            $res = City::where('name', 'LIKE', "%$query%")->orderBy('name')->paginate(5);
            foreach($res as $cities ){
                $usersArray[] = array(
                    "label" => $cities->name,
                    "value" => $cities->id,
                    "country" => $cities->countries->id,
                    "countryname" => $cities->countries->country
    
                );
            }
            return response()->json($usersArray);
        }
    

    JS:

    $('#sl').autocomplete({
        source: '/autocomplete',
        select: function(event, ui) {
            event.preventDefault();
            $("#country").val(ui.item.country); // save selected id to hidden input
            $("#city").val(ui.item.value); // save selected id to hidden input
            $('#sl').val(ui.item.label +', '+ ui.item.countryname)
    
        },
        focus: function(event, ui){
    
            event.preventDefault();
            $('#sl').val(ui.item.label+', '+ui.item.countryname);
    
        },
        response: function(event, ui) {
            if (!ui.content.length) {
            var noResult = { value:"",label:'No results found', countryname:""  };
            ui.content.push(noResult);
    
        }
    }
    }).autocomplete( "instance" )._renderItem = function( ul, item ) {
        var li = $("<li>");
        if (item.country == undefined) {
            li.append("<div>" + item.label +"</div>");
        } else {
            li.append("<div><strong>" + item.label + "</strong>, " + item.countryname + "</div>");
        }
        return li.appendTo(ul);
    };
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Twisty    6 年前

    以下是如何禁用选择项的示例: http://jqueryui.com/autocomplete/#categories

    这个例子有点复杂,因为我们在自动完成小部件中访问菜单小部件。对于您的代码,可以使用类似的方法:

    $("#sl").autocomplete("widget").menu("option", "items", "> li:not('.no-select')");
    

    widget()

    返回包含菜单元素的jQuery对象。尽管菜单项是不断创建和销毁的,但菜单元素本身是在初始化过程中创建的,并不断重复使用。

    这涉及到第1点。

    为了解决第2点,如果用户没有进行选择,则需要考虑假设选择的逻辑。例如,如果用户键入 l

    在我看来,最好检查隐藏字段,如果它们是空的,则阻止提交表单,并强制用户选择一个选项,使其成为必填字段。

    $(function() {
    
      var countries = [{
        country: 1,
        countryname: "UK",
        label: "London",
        value: 1
      }, {
        country: 1,
        countryname: "UK",
        label: "Manchester",
        value: 2
      }];
    
      $('#sl').autocomplete({
        source: countries,
        select: function(event, ui) {
          event.preventDefault();
          if (ui.item.label === "No results found") {
            $("#sl").val("");
            return false;
          }
          $("#country").val(ui.item.country); // save selected id to hidden input
          $("#city").val(ui.item.value); // save selected id to hidden input
          $('#sl').val(ui.item.label + ', ' + ui.item.countryname)
        },
        focus: function(event, ui) {
          event.preventDefault();
          $('#sl').val(ui.item.label);
        },
        response: function(event, ui) {
          if (!ui.content.length) {
            var noResult = {
              value: "",
              label: 'No results found'
            };
            ui.content.push(noResult);
          }
        }
      });
    
      $("#sl").autocomplete("widget").menu("option", "items", "> li:not('.no-select')");
    
      $("#sl").autocomplete("instance")._renderItem = function(ul, item) {
        var li = $("<li>");
        if (item.country == undefined) {
          li.addClass("no-select").append(item.label);
        } else {
          li.append("<div><strong>" + item.label + "</strong>, " + item.countryname + "</div>");
        }
        return li.appendTo(ul);
      }
    
      $("form").submit(function(e) {
        e.preventDefault();
        console.log($(this).serialize());
        if ($("#country").val() == "" || $("#city").val() == "") {
          $("#sl").focus();
          return false;
        }
        return true;
      });
    });
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    <div class="search-homepage-input">
      <form>
        <div class="col-md-9">
          <input type="text" name="sl" class="form-control shop-input" max-length="50" placeholder="Eg. England, London, Manchester" id="sl" /> <span style="color: red; font-size: 65%;">* Required</span>
          <input type="text" name="country" id="country" style="display: none;" />
          <input type="text" name="city" id="city" style="display: none;" />
        </div>
        <div class="col-md-3">
          <button type="submit" class="btn btn-homepage">Find Teams</button>
        </div>
      </form>
    </div>

    希望有帮助。