2

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

当用户修改原始选择时会发生什么?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其他字段需要清除。

另一种提问方式:我如何处理“新”列表?

我的代码如下

$(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);
        }
    });
});

组织者列表.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));

我的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; ?>">
4

1 回答 1

0

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

用户选择选项后禁用输入

$("#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/

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

于 2012-06-13T16:22:19.310 回答