0

我有以下 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);
}); 
4

1 回答 1

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>
于 2012-09-21T02:10:10.810 回答