jQuery自动完成:如何处理修改? [英] JQuery Auto-Complete: How do I handle modifications?
问题描述
我有自动完成的工作,但是如何处理修改?
I have auto-complete working, but how do I handle modifications?
当用户修改原始选择时会发生什么?我有一个自动完成的是,选择的列表时,其他字段填写.如果用户选择的列表,然后尝试修改它的东西是新的(不匹配,我们的DB任何东西),该其他字段需要清除.
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?
我的下面的代码
$(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));
我的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; ?>">
推荐答案
您可以使用自动完成select
事件 http://jqueryui.com/demos/autocomplete/#event-select
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.
这篇关于jQuery自动完成:如何处理修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!