简体   繁体   English

在鼠标按下/输入时重置文本字段 - jQuery UI 自动完成

[英]Reset text field on Mouse press / Input - jQuery UI Autocomplete

Simple JS fiddle containing my code in working state包含我在工作 state 中的代码的简单 JS 小提琴

I have a jQuery UI Autocomplete field , with a Dropdown button attached.我有一个jQuery UI 自动完成字段,附加了一个下拉按钮。 It works floorlessly, however - its kinda annoying you have to manually delete the words inside the field for a search.但是,它的工作原理很简单-您必须手动删除字段内的单词才能进行搜索,这有点烦人。

I am unsure if jQuery UI has a feature for it, unless i'd love to know.我不确定jQuery UI是否有它的功能,除非我很想知道。

I've tried to use onClick functions with JS, however since my field is not exactly an "form field" I've got kinda lost here.我尝试将 onClick 函数与 JS 一起使用,但是由于我的字段不完全是“表单字段”,所以我在这里有点迷失了。

My goal is to: reset the text field when a user presses it.It has prewritten text in it "Please select (Or Type)"我的目标是:当用户按下它时重置文本字段。它里面有预先写好的文本“请 select(或类型)”

my cshtml file looks as following cshtml我的 cshtml 文件如下所示 cshtml

And it looks like this on the browser browser它在浏览器浏览器上看起来像这样

Code for Image 1:图 1 的代码:

<select asp-for="Dinosaur" class="combobox" id="dinoType" asp-items="Html.GetEnumSelectList<Dinosaurs>()">
        <option selected="selected" type="text" onclick="resetText()" value="0">Please select (Or Type)</option>
    </select>
    <span asp-validation-for="Dinosaur" class="text-dark" />

As you can see it has the text in, which i have to CTRL + A, DELETE before i can search in my field.如您所见,其中包含文本,我必须先按 CTRL + A、DELETE 才能在我的字段中搜索。

A function to clear this text when a user presses it will easen the pressure. function 在用户按下时清除此文本将减轻压力。

I might just be stupid to see the simple solution, i just feel like I've tried some of the things that I'd believe would work.我可能只是愚蠢地看到了简单的解决方案,我只是觉得我已经尝试了一些我认为可行的方法。 (As the onclick="ResetText()" with a JS code attached to it) (作为附有 JS 代码的 onclick="ResetText()")

When I click on drop down this is what showing.当我单击drop down就是显示的内容。

Best Regards,此致,

You don't want to wire an onclick listener on your option element, you want an onchange event listener on your select element.您不想在选项元素上连接 onclick 侦听器,而是希望在 select 元素上连接 onchange 事件侦听器。 onclick is not supported on option elements.选项元素不支持 onclick。

use onchange instead of using onclick and this action should be on the select tag.使用onchange而不是使用onclick并且此操作应在select标记上。 not on the options.不在选项上。 Try this example.试试这个例子。

 $('select').on('change', function() { if (this.value === 'disabled') { this.value = ''; } console.log(this.value); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select asp-for="Dinosaur" class="combobox" id="dinoType" asp-items="Html.GetEnumSelectList<Dinosaurs>()"> <option selected="selected" type="text" value="disabled">Please select (Or Type)</option> <option type="text" value="One">One</option> <option type="text" value="two">two</option> </select> <span asp-validation-for="Dinosaur" class="text-dark" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM