简体   繁体   English

jQuery自动完成更改事件

[英]JQuery autocomplete change event

I got a jquery autocomplete combobox on my page like this: 我在页面上得到一个jQuery自动完成组合框,如下所示:

$.widget( "ui.combobox", {
        _create: function() {
        ...
        var input = this.input = $( "<input>" )
                .insertAfter( select )
                .val( value )
                .autocomplete({
                    ...
                    select: function( event, ui ) {
                    ...
                    change: function( event, ui ) {
                    ...

$(function() {
    $("#myid").combobox();
}

on select and change events I perform ajax calls and change some info in the hidden fields, but the problem appear when I just type something and without unfocusing the text field click Submit button - change event have not being triggered and I get a wrong data on server (basically I get a data in hidden fields which was previously set by select-change fired events, but have no catch the latest). selectchange事件上,我执行ajax调用并更改隐藏字段中的某些信息,但是当我只键入内容并且使文本字段失去焦点时,就会出现问题,请单击“提交”按钮- change事件尚未触发,并且我收到了错误的数据服务器(基本上我在隐藏字段中获得了一个数据,该数据先前是由select-change触发事件设置的,但是没有捕获最新的数据)。 So how can I perform text field unfocus to trigger change event before sending a form? 那么,在发送表单之前,如何执行文本字段模糊处理来触发change事件? Or maybe there are some other ways to catch and trigger latest changes in text field(mb replace change by some onkeypress event)? 或者,也许还有其他方法可以捕获和触发文本字段中的最新更改(用某些onkeypress事件替换mb)? Thanks for any help. 谢谢你的帮助。

You could disable the default action on the submit button 您可以禁用“提交”按钮上的默认操作

$(".submit-btn").click(function(e){e.preventDefault();});

This way the submit button wont submit the form and your form will get the select or change event coz the user will have unfocussed by clicking the submit button 这样,提交按钮将不会提交表单,并且您的表单将获得选择或更改事件,因为用户将无法通过单击提交按钮来集中精力

What you can do is make a separate out the function for fetching hidden field data on change event. 您可以做的是单独创建一个功能,用于在更改事件时获取隐藏的字段数据。 Also, have a callback function as a parameter to that function which will get called after data has been updated. 另外,将回调函数作为该函数的参数,在数据更新后将调用该函数。 Then you can use the same function in change event handler and in submit handler for form. 然后,您可以在更改事件处理程序和表单的提交处理程序中使用相同的功能。 In submit handler, you will disable default submit action and also provide callback to that function so that when data is updated in hidden fields you can submit the form through JS. 在提交处理程序中,您将禁用默认的提交操作,并提供对该函数的回调,以便在隐藏字段中更新数据时,您可以通过JS提交表单。

Above solution should work, but if you are fetching data to fill out from your own servers then you should not do that through Javascript on client but that should happen on your server side code only. 上面的解决方案应该可以工作,但是如果您要获取数据以从自己的服务器中填写数据,则不应通过客户端上的Javascript来做到这一点,而应仅在服务器端代码上进行。 For example if you are using hidden fields to map user entered string eg "poland" to some id in your database and storing that ID in hidden field then you should simply send "poland" as string to server and handle this mapping on server. 例如,如果您使用隐藏字段将用户输入的字符串(例如“ poland”)映射到数据库中的某个ID,并将该ID存储在隐藏字段中,则您只需将“ poland”作为字符串发送到服务器并在服务器上处理此映射。 That way your form need not wait to get this mapping from server. 这样,您的表单就无需等待从服务器获取此映射。 Of course this can only be done if you are sending AJAX request to your server and not to some other server which you can not access from your server. 当然,只有在将AJAX请求发送到服务器而不是其他无法从服务器访问的服务器时,才能完成此操作。

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

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