jQuery自动完成onChange事件 [英] Jquery Autocomplete onChange event

查看:103
本文介绍了jQuery自动完成onChange事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的php页面中有一个列表菜单.在此列表菜单中,我有一个onChange事件,您可以在下面看到.

I have a list menu in my php page. In this list menu i have an onChange event, you can see below.

<select name="company" onChange="getproduct(this)">

以上事件获取选定公司的产品名称.但现在我想将此列表菜单更改为自动完成.我尝试下面的代码,但是它什么也没得到,有人可以帮助我吗

The above event get product name of selected company. but now i want to change this list menu to autocomplete. I try below code but its not fetching anything, can anyone help me plz

<input type="text" name="company" id="company"  onkeyup="getproduct(this)"/>

推荐答案

通常来说,对javascript事件使用html属性是不好的做法.由于仍然使用jQuery,因此您应该在外部脚本中执行以下操作:

Generally speaking, using the html attributes for javascript events is bad practice. Since you are using jQuery anyway, you should do something like this in an external script:

$('#company').on('change keyup input', function() { getproduct(this); });

我现在将其分解.

$('#company') 选择ID为"company"的元素.这样,您就可以通过更改或附加事件监听器来使用javascript处理该对象.

$('#company') selects the element that has an ID of "company." This lets you work with that object with javascript by changing it or attaching event listeners.

.on(...) 将事件侦听器附加到所选对象.它可以以几种不同的方式使用,但是最基本的方式(这就是我们在这里使用的方式),它需要两个参数,第一个是用空格分隔的事件列表,第二个是回调函数这些事件发生在选定元素上时执行.

.on(...) attaches event listeners to the selected objects. It can be used in a few different ways, but the most basic way (which is how we are using it here), it takes two parameters, the first is a space separated list of events to listen for and the second is a callback function to execute when these events happen on the selected element.

change 是一个事件,当输入元素更改其值并且然后失去了焦点.如果用户没有通过键入来更改输入(可能通过上下文菜单粘贴),这可能是一个有用的事件.

change is an event that is triggered when the input element has changed its value and then lost focus. This can be a useful event to use in case the user didn't change the input by typing (possibly pasting through the context menu).

keyup 是在按下某个键然后释放时触发的事件.另请参见keydownkeypress.

input是一个很酷的新HTML5事件,旨在监听元素上的用户输入.使用这一点很重要,因为它是专门为您要完成的任务而设计的. (值得注意的是,在数字输入类型上单击微调箭头按钮时会触发输入事件.)如果用户的浏览器不支持输入事件,您仍然希望将其他事件用作后备.

input is a cool new HTML5 event that is designed to listen to user input on an element. This is important to use because it was designed specifically for what you are trying to accomplish. (Notably, the input event is triggered when the spinner arrow buttons are clicked on the number input types.) You still want to use the other events as fallbacks in case the input event isn't supported in the user's browser.

这篇关于jQuery自动完成onChange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆