jquery自动完成根据数据库的值改变背景颜色。

问题描述 投票:0回答:1

我想改变列表的背景颜色,当我在数据库中的产品数量小于或等于1时,这工作正常,只是不知道如何改变背景。

<input type="text" value=""  id="mercaderia"   >


$("#mercaderia").autocomplete({
     source: "autocompletemer.php",         
     minLength: 2,
     html: 'html',
     focus: function(event, ui) {
            // prevent autocomplete from updating the textbox
            //event.preventDefault();
            // manually update the textbox
            //$(this).val(ui.item.label);
        },
     select: function(event, ui) {
            // prevent autocomplete from updating the textbox
            //event.preventDefault();
            // manually update the textbox and hidden field
            //$(this).val(ui.item.label);
            //$("#autocomplete2-value").val(ui.item.value);
        }               
});

Intento hacerlo desde php pero no queda bien我试着从php中做,但不适合。

<?php
//defino una clase que voy a utilizar para generar los elementos sugeridos en autocompletar
class ElementoAutocompletar {
   var $value;
   var $label;

   function __construct($label, $value){
      $this->label = $label;
      $this->value = $value;
   }
}

//recibo el dato que deseo buscar sugerencias
$datoBuscar = $_GET["term"];

//conecto con una base de datos
$conexion = mysql_connect("localhost", "root", "");
mysql_select_db("base_ropa");

//busco un valor aproximado al dato escrito
$ssql = "SELECT id, titulo, precio, cantidad FROM mercaderia WHERE titulo LIKE '%" . $datoBuscar . "%' LIMIT 10";
$rs = mysql_query($ssql);

//creo el array de los elementos sugeridos
$arrayElementos = array();

//bucle para meter todas las sugerencias de autocompletar en el array
while ($fila = mysql_fetch_array($rs)){
    $temp = $fila["titulo"].":".str_replace('.',',',$fila[precio]).":".$fila[cantidad];

    $t = explode(":", $temp);
    if ($t[2] <= 1) {
        $temp1 = str_replace(strtolower($datoBuscar), '<span style="background-color:red">"<b class="ot4">'.$datoBuscar.'</b>', '<b>'.strtolower($t[0]).' - &lt;</b>');     
        $temp = str_replace('&lt;', '<span class="ot2"> $'.$t[1].'</span></span>', $temp1);
    }else {
        $temp1 = str_replace(strtolower($datoBuscar), '<b class="ot">'.$datoBuscar.'</b>', '<b>'.strtolower($t[0]).' - &lt;</b>');      
        $temp = str_replace('&lt;', '<span class="ot2"> $'.$t[1].'</span>', $temp1);
    }

            //$respuesta .= "<li style='background-color: #FFDDDD;'>".$temp."</li>";
        //else
            //$respuesta .= "<li>".$temp."</li>";



    array_push($arrayElementos, new ElementoAutocompletar($temp, $fila["titulo"].' - $'.$fila["precio"])); //$fila["id"]
}

print_r(json_encode($arrayElementos));
?>
jquery autocomplete
1个回答
0
投票

这样做不安全:$datoBuscar = $_GET["term"];

$ssql = "SELECT id, titulo, precio, cantidad FROM mercaderia WHERE titulo LIKE '%'" 。$datoBuscar . "%' LIMIT 10"。

这并没有回答你原来的问题,但这段代码可能会被破坏。

© www.soinside.com 2019 - 2024. All rights reserved.