JQuery自动完成:如何处理修改?

时间:2022-12-04 15:05:06

I have auto-complete working, but how do I handle modifications?

我有自动完成的工作,但我如何处理修改?

What happens when the user modifies the original selection? I've got an auto-complete that, when a listing is chosen, other fields are filled in. If the user chooses a listing, then tries to modify it to something that is new (does not match anything in our DB), the other fields need to be cleared.

当用户修改原始选择时会发生什么?我有一个自动完成功能,当选择列表时,会填充其他字段。如果用户选择了一个列表,然后尝试将它修改为一个新的内容(与我们的DB中的任何内容不匹配),则需要清除其他字段。

Another way of asking: How do I handle 'new' listings?

另一种问的方式是:我如何处理“新”清单?

My code below

我的代码如下

$(function() {
    $( "#oName" ).autocomplete({
        source: "include/organizerList.php",
        minLength: 3,
        select: function( event, ui ) {
            $("input#oID").val(ui.item.oID);
            $("input#oCID").val(ui.item.oCID);
            $("div#organCity").text(ui.item.oCity);
            $("div#organCountry").text(ui.item.oCountry);
        }
    });
});

organizerList.php

organizerList.php

// important to set header with charset
header('Content-Type: text/html; charset=utf-8');

$term = htmlspecialchars(strtolower($_GET["term"]));

$return = array();
    $query = mssql_query("SELECT CID, oID, oName, oCity, oCountry FROM TradeShowDB_Organizers WHERE oName LIKE '%$term%'");
    while ($row = mssql_fetch_array($query)) {
    array_push($return,array( 'oCID'=>$row['CID'], 'oID'=>$row['oID'], 'label'=>$row['oName'] . ', ' . $row['oCity'], 'value'=>$row['oName'], 'oCity'=>$row['oCity'], 'oCountry'=>$row['oCountry'] ));
}

// encode it to json format
echo(json_encode($return));

My html:

我的html:

<input type="text" tabindex='20' id="oName" name="oName" size="60" maxlength="200" value="<?php echo $oName; ?>">
<div id='organCity'></div>
<div id='organCountry'></div>
<input type="hidden" id="oID" name="oID" value="<?php echo $oID; ?>">
<input type="hidden" id="oCID" name="oCID" value="<?php echo $oCID; ?>">

1 个解决方案

#1


0  

You can use the autocomplete select event http://jqueryui.com/demos/autocomplete/#event-select

您可以使用autocomplete select事件http://jqueryui.com/demos/autocomplete/#event-select

Disable the input once the user selects an option

当用户选择一个选项时,禁用输入

$("#oName").autocomplete({
    source: "include/organizerList.php",
    minLength: 3,
    select: function (event, ui) {
        this.value = ui.item.value;
        $('#autocomplete').autocomplete("disable");
        $('#autocomplete').trigger('keypress'); //needed to fix bug with enter on chrome and IE
        $(this).attr('disabled','disabled');
        return false;
    },
    autoFocus: true
});

http://jsfiddle.net/HKxua/6/

http://jsfiddle.net/HKxua/6/

Then in your server side script, you can check the input to see if the value posted exists in the database.

然后,在您的服务器端脚本中,您可以检查输入,以查看提交的值是否存在于数据库中。

#1


0  

You can use the autocomplete select event http://jqueryui.com/demos/autocomplete/#event-select

您可以使用autocomplete select事件http://jqueryui.com/demos/autocomplete/#event-select

Disable the input once the user selects an option

当用户选择一个选项时,禁用输入

$("#oName").autocomplete({
    source: "include/organizerList.php",
    minLength: 3,
    select: function (event, ui) {
        this.value = ui.item.value;
        $('#autocomplete').autocomplete("disable");
        $('#autocomplete').trigger('keypress'); //needed to fix bug with enter on chrome and IE
        $(this).attr('disabled','disabled');
        return false;
    },
    autoFocus: true
});

http://jsfiddle.net/HKxua/6/

http://jsfiddle.net/HKxua/6/

Then in your server side script, you can check the input to see if the value posted exists in the database.

然后,在您的服务器端脚本中,您可以检查输入,以查看提交的值是否存在于数据库中。