简体   繁体   English

从websql访问数据

[英]accessing data from websql

Good Morning, I have a script that retrieves data from a WebSQL db, it works fine. 早上好,我有一个脚本可以从WebSQL数据库中检索数据,它可以正常工作。

$(document).ready(function(e) {
var v= location.search.replace("?init=", "");
document.getElementById('key').innerHTML=v;

var db = openDatabase('contacts', '1.0', 'contacts database', 5 * 1024 * 1024); var x = v + "%";    
 db.transaction(function(tx){   
    tx.executeSql('SELECT * FROM names WHERE  lname  LIKE "' + x +'"', [], function (tx, results){  
  var len = results.rows.length, i;
  if(len > 1 ){   // if len is greater than 1 
  for (i = 0; i < len; i++) {
        $('#nameList').append("<li data-url='autolkp.html?id='" + results.rows.item(i).lname + "' class='n' id='"+results.rows.item(i).lname+"'>" + results.rows.item(i).lname + "</li>");
  }
  } // closes if len is greater than 1
else {  
        $("#fName").val(results.rows.item(0).fname);    
        $("#lName").val(results.rows.item(0).lname);
        $("#adx").val(results.rows.item(0).adx);
        $("#city").val(results.rows.item(0).city);
        $("#state").val(results.rows.item(0).st);
        $("#zip").val(results.rows.item(0).zip);
        $("#phone1").val(results.rows.item(0).phone1);
        $("#phone2").val(results.rows.item(0).phone2);
        $("#email").val(results.rows.item(0).email);
}   });   });  });

this script takes the results from the query and creates a list with the last names. 该脚本从查询中获取结果,并创建一个包含姓氏的列表。

I am trying to access the data-url from the li with this script 我正在尝试使用此脚本从li访问data-url

$(document).ready(function(){
$('li').click(function(){
    a = $(this).attr('data-url');
    alert(a);
}); });

it isn't working. 它不起作用。 when you do a view page source - none of the data from the executeSql is visible, but when you go into console it is. 当您执行视图页面源时-executeSql的所有数据都不可见,但进入控制台时则可见。 Is this a timing issue? 这是时间问题吗? How can I get around it? 我该如何解决?

Thx. 谢谢。

You're setting the <li> click listener before it renders the results. 您要设置<li>单击侦听器,然后才能呈现结果。 You just need to change the click listener to this: 您只需要将点击侦听器更改为此:

$(document).ready(function () {
  $('#nameList').on('click', 'li', function() {
    // DO SOMETHING
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM