jQuery自动完成动态行

时间:2012-09-20 20:54:55

标签: php javascript jquery autocomplete jquery-autocomplete

我有以下jQuery代码:

$(document).ready(function(){
var ac_config = {
    source: "autocomplete-delta.php",
    select: function(event, ui){
        $("#del_item").val(ui.item.SKU);
        if ((ui.item.CASE_PRICE) != "N/A"){
            $("#del_price").val(ui.item.CASE_PRICE);
        } else {
            $("#del_price").val(ui.item.UNIT_PRICE);
        }
    },
    minLength:1
};
$("#del_item").autocomplete(ac_config);
});

对于一个订单项适用,基本上该订单项会使用商品名称,即您为自动填充键入的字段,然后在选择后,使用我的数据库中的单价或个案价格填写价格字段。现在我希望通过php设置的这些行中的18个是del_item1,del_item2等等。当我尝试以下代码时,自动完成工作并且它填写项目很好,但是价格字段没有填写,任何想法......?

$(document).ready(function(){
for (var i = 1; i < 19; i++) {  
    var ac_config = {
    source: "autocomplete-delta.php",
    select: function(event, ui){
        $("#del_item" + i).val(ui.item.SKU);
        if ((ui.item.CASE_PRICE) != "N/A"){
            $("#del_price" + i).val(ui.item.CASE_PRICE);
        } else {
            $("#del_price" + i).val(ui.item.UNIT_PRICE);
        }
    },
    minLength:1
};
$("#del_item" + i).autocomplete(ac_config);
}); 

1 个答案:

答案 0 :(得分:0)

这是JS引用的php文件:

<?php
require_once "/home/default/support/default.php";
$dbh = showDB ();
$cities = array();
$sth = $dbh->prepare("SELECT * FROM purchase_items");
$sth->execute();

 while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$cities[]=$row;
 }

$term = trim(strip_tags($_GET['term']));

$matches = array();
foreach($cities as $city){
if((stripos($city['SKU'], $term) !== false) || (stripos($city['FAMILY'], $term) !== false) || (stripos($city['DESCRIPTION'], $term) !== false)){
    // Add the necessary "value" and "label" fields and append to result set
    $city['value1'] = $city['SKU'];
    $city['value2'] = $city['FAMILY'];
    $city['value3'] = $city['DESCRIPTION'];
    $city['label'] = "{$city['FAMILY']} - {$city['DESCRIPTION']} ({$city['SKU']})";
    $matches[] = $city;
}
}
$matches = array_slice($matches, 0, 100);
print json_encode($matches);

以下是上面要求的html方面,这些是我正在使用的16个项目,在for循环中,循环中的计数器是$ d:

if($d&1) { ?>
<div id="trow">
<? } else { ?>
<div id="trow" class="none">
    <? } ?>
        <div id="thirds" style="text-indent:5px;">
    <input type="text" name="del_item<? echo("$d");?>" id="del_item<? echo("$d");?>" class="salesinput"
    placeholder="Start typing item SKU, family, description..." style="text-align:left; width:95%;" />
    </div>
    <div id="dollar"><span class="tdblackbigger">$ </span></div>
    <div id="lfamt" style="width:15%;"><input type="text" name="del_price<? echo("$d");?>" id="del_price<? echo("$d");?>" class="salesinput" style="text-align:right; padding-right:5px;" /></div>
    <div id="lfsold" style="width:15%;"><input type="text" name="del_retail<? echo("$d");?>" id="del_retail<? echo("$d");?>" class="salesinput" /></div>
    <div id="dollar"><span class="tdblackbigger">$ </span></div>
    <div id="lfamt" style="width:15%;"><input type="text" name="del_line<? echo("$d");?>" id="del_line<? echo("$d");?>" class="salesinput" style="text-align:right; padding-right:5px;" /></div>
       </div>