简体   繁体   English

具有动态行的jQuery自动完成

[英]jQuery Autocomplete with dynamic rows

I have the following jQuery code: 我有以下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);
});

Which works fine for one line item, basically the line item takes an item name, which is the field you type in for autocomplete and then after selecting it fills the price field with either the unit price or case price from my DB. 这对于一个订单项正常工作,基本上,该订单项具有一个项目名称,这是您键入的用于自动完成的字段,然后在选择它后用我数据库中的单价或案例价格填充价格字段。 Now I want to have 18 of these rows which I set up through php to be del_item1, del_item2 etc. and when I tried the following code, the autocomplete works and it fills in the item fine, but the price field does not fill in, any ideas...? 现在,我想将其中的18行通过php设置为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);
}); 

Here is the php file that the JS references: 这是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);

And here is the html side as requested above these are the 16 line items I'm working with, inside of a for loop the counter in the loop is $d: 这是上面要求的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM