如何让选择列表数据的ID在jQuery Mobile的自动完成 [英] how to get id of selected list data in the jquery mobile autocomplete

查看:156
本文介绍了如何让选择列表数据的ID在jQuery Mobile的自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建中,我已经使用jQuery Mobile的自动完成从数据库中自动完成列表视图code是在js文件返回动态创建的列表视图数据的应用程序。我能够从列表视图中选择数据并显示在自动完成输入字段,然后禁用搜索列表。知道我要的是作用似乎我是您可以通过数据库中的数据时,用户选择列表中的数据将显示的名字,但是当它点击thesave按钮,它应该保存名称,而不是在jQuery用户界面自动完成名称的代号,易BCZ那里我们可以使用阵列标签和值。但我不知道怎么做,在jQuery Mobile的。
在code在这里创建自动完成:

I am creating an app in which i have used jquery mobile autocomplete the listview data is created dynamically from the database the autocomplete listview code is return in the js file. I am able to select the data from the listview and show it in the input field of the autocomplete and then disable the search list. Know what i want is seens i am get the data from the data base when user select the list data it will display the name but when it click on thesave button it should save the id of that name instead of the name in jquery ui autocomplete its easy bcz there we can use array with label and value. But i dont know how to do it in jquery mobile. The code for creating the autocomplete in here:

var db = window.openDatabase("Database", "1.0","BPS CRM", 200000);
$( document ).on( "pageinit", "#myPage", function() {

var usrname=new Array();
$( "#autocomplete" ).on( "listviewbeforefilter", function ( e, data ) {
    var $ul = $( this ),
        $input = $( data.input ),
        value = $input.val(),
        html = "";
    $ul.html( "" );
    db.transaction(function(tx){
        tx.executeSql('select * from users',[],function(tx,results){
            var dataset=results.rows;
            if(dataset.length>0){
            for(var i=0;i<dataset.length;i++){
                    usrname[i] =dataset.item(i).user_name;
                }
            }
        });
    });
    if ( value && value.length > 1 ) {
                    for(var j=0;j<usrname.length;j++){
                    html +="<li>"+usrname[j]+"</li>";
                }

         $ul.html( html );
         $ul.listview( "refresh" );
         $ul.trigger( "updatelayout");

         $.mobile.activePage.find('input[placeholder="Find a city..."]').attr('id','namesearch');
    }

    $("ul>li").click(function(){

        var textval=$(this).text();
        $('#namesearch').val(textval);
        $.mobile.activePage.find("[data-role=listview]").children().addClass('ui-screen-hidden');

    });
}); });

这将巨大的,如果小例子给出了相同
任何建议AP preciated提前Thnaks。

It will great if small example is given for the same Any suggestion is appreciated Thnaks in advance.

推荐答案

我不是很确定我理解的问题,但我认为你正在寻找的是用户ID存储在&LT ;丽方式&gt; 元素,除了具有用户名作为显示的文本

I am not exactly sure I understood the question, but I think what your are looking for is to store the user ID in the <li> elements, in addition to having the username as the displayed text.

这样的事情可能会为你做的工作:

Something like this might do the job for you:

// declare a collection of Ids where you declare usrname
var usrids = new Array();

// in your execute sql callback, add this line:
usrids.push(dataset.item(i).user_id);  // see note below

// in your for loop where you insert li's, modify the line like this :
html +="<li data-userid='" + usrids[j] + "'>"+usrname[j]+"</li>";

// then in your ul>li click event, you can reference your ids like this :
var userId = $(this).data('userid');

此外,作为一个一般的说明,我认为这是更好地使用usrids.push(...)和usrname.push(...)而不是使用usrids [I] = ...;当元素我已经不给定数组中存在。

Also, as a general note, I think it's better to use usrids.push(...) and usrname.push(...) instead of using usrids[i] = ...; when the element i does not already exist in the given array.

这篇关于如何让选择列表数据的ID在jQuery Mobile的自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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