jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 将输入框转换为combobox以进行jQuery自动完成大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
代码正常运行.但是,我需要帮助让“输入”标签像组合框一样显示.我试图将输入框设计为组合框而没有成功.我也在寻找一种通过动态构建选项使代码与组合框一起工作的方法.任何和所有的帮助表示赞赏.

$(function () {
  var availableTags = new Array(1000000);
  var j = 0;
  for (var i = 1; i < availableTags.length; i++) {
    availableTags[j++] = i.toString();
  }
  $("#tags").autocomplete({
    source: function (req,responseFn) {

      var re = req.term;
      var matcher = new RegExp("^" + re,"i");
      var a = $.grep(availableTags,function (item) {
        return matcher.test(item);
      });
      responseFn(a.slice(0,5));
    },SELEct: function (event,ui) {
      if (ui.item && ui.item.value) {
        var titleinput = ui.item.value;
        ui.item.value = $.trim(titleinput);
        alert(ui.item.value);
      }
    },change: function (event,ui) {
      if (!valid) {
        // remove invalid value,as it didn't match anything
        $(this).val("");
        SELEct.val("");
        return false;
      }
    }

  });
});
.custom-comboBox {
  position: relative;
  display: inline-block;
  border-style: solid;
  border-width: 1px;
}
.custom-comboBox-toggle {
  position: absolute;
  top: 0;
  bottom: 0;
  margin-left: -1px;
  padding: 0;
}
.custom-comboBox-input {
  margin: 0;
  padding: 5px 10px;		
}

/*
.custom-comboBox-list-item {
  color: blue;
  font-weight: bold;
}
.custom-comboBox-input,.custom-comboBox-list-item.ui-state-focus {
  color: black;
  font-weight: bold;
  font-style: italic;
}
*/

#tags {
  width: 40px;
}
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>

<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">

<label for="tags">Tags: </label>
<input id="tags" type="text" class="custom-comboBox" value="">

解决方法

您可以使用数据列表将选项传输到文本输入,然后将其转换为组合框

我简化了你的代码,写下了基础知识来做一个简单的例子.

<label for="tags">Tags: </label>
<input id="tags" name="tags" type="text" list="options" class="custom-comboBox" value="">

<datalist id="options">
    <!-- it puts the options here then the input type text gets them as SELEct options -->
</datalist> 

$(function () {
  var availableTags = new Array(10);
  var j = 0;
  for (var i = 1; i < availableTags.length; i++) {
    $('#options').append("<option value='" + i + "'>");
  }

});

这是一个显示基本功能JSFIDDLE

大佬总结

以上是大佬教程为你收集整理的javascript – 将输入框转换为combobox以进行jQuery自动完成全部内容,希望文章能够帮你解决javascript – 将输入框转换为combobox以进行jQuery自动完成所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。