0

好的,所以我对此有点陌生..我已经用谷歌搜索了这个问题,但我只是不明白。

我有 2 个输入,描述和条形码

我需要在描述字段中搜索,并在选择描述建议时自动完成描述和条形码。

到目前为止,这是我所拥有的,我似乎无法弄清楚如何将 php 数组添加到 Jquery 中(允许没有显示数组,这是我用于单个输入字段的工作代码)有人请帮我把这个搞砸。

    <?php //My Inputs (are in a loop) *** ?>

    <input name="<?php echo 'ingdes'.$y ?>" type="text"  value="<?php  issetValue($ingdes[$y]) ?>"class="autosearch" id="<?php echo 'ingdes'.$y ?>" size="45" autocomplete='off'  /> <!-- THIS IS THE DESCRIPTION -->

     DESC

     <input name="<?php echo 'ingcode'.$y ?>" type="text" value="<?php issetValue($ingcode[$y]) ?>" class="code" id="<?php echo 'ingcode'.$y ?>" size="12"  /> 
     BARCD   <!-- THIS IS THE BARCODE -->

    <?php // End My inputs ***

          // My function ***

          public function jsAutoSearch() { ?>

          <script>
          $(function() {
          var availableTags = [ <?php

          $query= mysql_query("SELECT * FROM $this->usernameid ORDER BY name");

          while (($row = mysql_fetch_assoc($query)) !== false){
          echo '"'.$row['name'].'",';}?> 
          /* $row['barcode1'] is the additional field I need to get working and send to the other input field  */  

          ""];
          $( ".autosearch" ).autocomplete({
          source: availableTags
          });
          });
          </script>

          <?php }

          // End my function

我对数组的尝试* ** * ****

    public function jsAutoSearch() { 

       $return_arr = array();
    $query= mysql_query("SELECT * FROM $this->usernameid ORDER BY name");

    while (($row = mysql_fetch_assoc($query)) !== false){
    $row_array['name']  = $row['name']; // THIS IS THE DESCRIPTION
    $row_array['barcode'] = $row['barcode1']; // THIS IS THE BARCODE
          array_push( $return_arr, $row_array );
    } ?>
    <script>

    $( ".autosearch" ).autocomplete({
        source: "<?php echo json_encode($return_arr),'\n'?>",
    select: function (event, ui) {
    var item = ui.item;
    if(item) {
    $(".autosearch").val(item.name); <!-- THIS IS THE DESCRIPTION -->
    $(".ingcode1").val(item.barcode); <!-- THIS IS THE BARCODE -->
    }
    ;}
    })

</script>


<?php }
4

1 回答 1

0
     <?//*****************search.php*********************

     $searchterm = $_GET['term'];
   $return_arr = array();
$query= mysql_query("SELECT * FROM $object1->usernameid WHERE name LIKE    '%$searchterm%' ORDER BY name");

 while (($row = mysql_fetch_assoc($query)) !== false){
       $names[] = array( name => $row['name'], barcode => $row['defcode'] ) ; 
    } 


 // Cleaning up the term
$term = trim(strip_tags($_GET['term']));
// Rudimentary search
$matches = array();
foreach($names as $name){
if(stripos($name['name'], $term) !== false){
    // Add the necessary "value" and "label" fields and append to result set
    $name['value'] = $name['name'];
    $name['label'] = "{$name['name']}, {$name['defcode']}";
    $matches[] = $name;
}
  }
 // Truncate, encode and return the results
 $matches = array_slice($matches, 0, 5);
 print json_encode($matches);?>

//********************** JQuery *****************************************

       <script type="text/javascript">

     $(document).ready(function(){
var ac_config = {
    source: "search.php",
    select: function(event, ui){
        $("#ingdes<?php echo $y ?>").val(ui.item.name);
        $("#ingcode<?php echo $y ?>").val(ui.item.barcode);

    },
    minLength:1
};
$("#ingdes<?php echo $y ?>").autocomplete(ac_config);
   });
 </script>
于 2013-01-07T01:00:48.647 回答