如何在 jquery ui 自动完成源事件中设置 id [英] how to set id in jquery ui autocomplete source event

查看:18
本文介绍了如何在 jquery ui 自动完成源事件中设置 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自动完成小部件来允许某人通过在文本框中输入员工姓名来选择员工,但我希望表单发布员工的 ID,而不是员工姓名.我提供了数据即员工姓名作为来源..js 中的标签和值与我提供的来源相同,我如何才能将员工姓名和 ID 分开.

I wanted to use the auto complete widget to allow someone to select an employee by typing in an employee name into the text box, but I want the form to post the ID of the employee, not the employee name.I provided the data ie the employee names as source. the label and value in the .js is same as the source i provided how can i possible to get employee name and id separate.

  $("#txtEmployeeName").autocomplete({  
                   var suggestions =  [] ; 
                   //define callback to format results  
                   source: function(req, add){  
                   ajax call...                 
                   on success: function( data ) 
                   {                   
                      suggestions = data.split('|');
                      add(suggestions);
                   } 
                   select: function(e, ui) { 
                     //create formatted friend  
                     var friend = ui.item.value,
                         span = $("<span>").text(friend)  
                         $("#"+ $(this).attr("id")).val(span);  
                    } 

                });   

推荐答案

您需要将一个 json 对象传递给源属性.

You need to pass a json object to the source propertie.

那么你的 ui.item 就是对象,有 Id,值,你想要的一切.

Then your ui.item is the object, with Id, value, everything you want.

$(function() {                  
    var students = [{id: 1322,label: "student 1"},{id: 2,label: "Student 2"}];
    $( "#search-student" ).autocomplete({
        source: students,
        minLength: 2,
        select: function( event, ui ) {
              window.location.href="/?studentId=" + ui.item.id;
        }                   
    });             
});

看jQuery的getJSON:http://api.jquery.com/jQuery.getJSON/ 通过ajax获取json.

Look at jQuery's getJSON : http://api.jquery.com/jQuery.getJSON/ to get json via ajax.

这篇关于如何在 jquery ui 自动完成源事件中设置 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆