JQuery Auto-Complete: How do I handle modifications?

China☆狼群 提交于 2019-12-10 22:27:22

问题


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.

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

// 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:

<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:


You can use the autocomplete select event 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/

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



来源:https://stackoverflow.com/questions/11018946/jquery-auto-complete-how-do-i-handle-modifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!