2014-05-15 56 views
1

我正在使用JQuery自動完成從php中的數據庫中獲取數據。 當我輸入關鍵字時,我從數據庫獲得正確的結果。但是,我希望將這些數據的id分開(因爲我不想在標籤本身中使用id)。我的jQuery代碼lokks這樣的:jquery自動完成將標識作爲選定標籤

$("#referrer").autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
      url: "/ajax/ir_populate_referrer", 
      dataType: "json", 
      type: "POST", 
      data: { 
       keyword: request.term 
      }, 
      success: function(data){ 
       response($.map(data, function(item) { 
        //alert(item.label); 
        return { 
         label: item.label 
        } 
       })); 
      } 
     }) 
    } 
}); 

PHP後端:

$searchArray = array(); 
    while($search = $result->fetch()){ 
     $link = ''; 
     $link .= $search['id'].', '.$search['cus_firstname'].' '.$search['cus_lastname'].', '.$search['cus_email'].', '.$search['cus_phone1']; 

     array_push($searchArray, array('label'=> $link, 'value' => $keyword, 'id'=>$search['id'])); 
    } 

echo json_encode($searchArray); 

問題是我怎麼可以把ID比標籤本身,當用戶選擇特定建議其他HTML。我希望把id在HTML容器:

<input type='hidden' name='referrer_id' id='referrer_id' /> 

回答

2
$("#referrer").autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
      url: "/ajax/ir_populate_referrer", 
      dataType: "json", 
      type: "POST", 
      data: { 
       keyword: request.term 
      }, 
      success: function(data){ 
       response($.map(data, function(item) { 
        //alert(item.label); 
        return { 
         label: item.label, 
         value: item.value  // EDIT 
        } 
       })); 
      } 
     }) 
    } 
    select: function(event, ui) { 
     $("#referrer_id").val(ui.item.value); // ui.item.value contains the id of the selected label 
    } 
}); 
+0

不過,這是不是給我的推薦人的價值。 – user3609998

+0

首先,你是否得到了ui.item.value中的id? – Manishankar

+0

這是給我的唯一標籤。沒有價值。 – user3609998

0
$("#zipsearch").autocomplete({ 
        source: function(req,res) { 
         $.ajax({ 
          url: "json.php", 
          dataType: "json", 
          type: "GET", 
          data: { 
           term: req.term 
          }, 
          success: function(data) { 
           res($.map(data, function(item) { 
            return { 
             label: item.label, 
             value: item.value, 
             id: item.id 
            }; 
           })); 
          }, 
          error: function(xhr) { 
           alert(xhr.status + ' : ' + xhr.statusText); 
          } 
         }); 
        }, 
        focus: function(event, ui) { 
         $("#zipsearch").val(ui.item.label); 
         return false; 
         }, 
        select: function(event, ui) { 
         alert(ui.item.id); 
         $("#zipsearch").val(ui.item.label); 
         return false; 
        } 
       }); 
0
<head runat="server"> 
<title></title> 
<link href="Styles/jquery-ui.css" rel="stylesheet" type="text/css" /> 
<script src="Scripts/jquery-1.10.2.js" type="text/javascript"></script> 
<script src="Scripts/jquery-ui.js" type="text/javascript"></script> 
<style> 
    body 
    { 
     font-size: 70%; 
    } 
</style> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#<%=TextBox1.ClientID %>").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        url: "AutoComplete.asmx/AutoCompleteAjaxRequest", 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json; charset=utf-8", 
        data: "{ 'prefixText' : '" + $("#<%=TextBox1.ClientID %>").val() + "'}", 
        success: function (data) { 
         response($.map($.parseJSON(data.d), function (item) { 
          return { 
           label: item.UserNameToShow, 
           value: item.UserName, 
           id: item.UserId 
          } 
         })) 
        } 
       }); 
      }, 
      select: function (event, ui) { 
       $("#referrer_id").val(ui.item.id); // ui.item.value contains the id of the selected label 
      } 
     }); 
    }); 
</script> 

相關問題