【问题标题】:Jquery/Html5 function issue double click issueJquery/Html5函数问题双击问题
【发布时间】:2013-09-22 07:40:35
【问题描述】:

我是 HTML5 / JqueryMobile 的新手。 我有这个功能,但由于某种奇怪的原因,它给了我第二次调用的结果,通过 Onclick(),这是我的代码:

function ListDBValues() {
  if (!window.openDatabase) { 
    alert('Databases are not supported in this browser.'); 
    return; 
 }
$('#lbUsers').html(''); 
db.transaction(function(transaction) { 
   transaction.executeSql('SELECT * FROM productos order by titulo;', [], 
   function(transaction, result) { 
      if (result != null && result.rows != null) { 
        for (var i = 0; i < result.rows.length; i++) { 
          var row = result.rows.item(i); 
          $('#lbUsers').append('<div id="producto"><img id="imgprod" width="100" src="images/' + row.foto +'">' + row.recid + '.' + row.titulo + '<br>$' + row.precio + ' MXP<br><input class="cuadro" type="button" id="cb.row" name="item"  value="ORDENAR" onclick=AddValueToOrders(' + row.recid + ');></div>');

        } 
      } 
     },errorHandler); 
 },errorHandler,nullHandler);
 return; 
}

这是第二个功能:

 function AddValueToOrders(item) {  
    if (!window.openDatabase) { 
      alert('Databases are not supported in this browser.'); 
      return; 
    } 
    msga = "El item es: "+ item ;
    alert(msga);
    db.transaction(function(tx) { 
        /// veo si ya existe /////

        tx.executeSql('SELECT count(*) AS c FROM orders where prodid = '+ item +'  ', [], 
        function (tx, result) { 
               totprod = result.rows.item(0).c;
               //totprod = results.rows.item(0)['c'];

        });
    });

    var themessage = "Total producto: ";
    themessage = themessage + totprod ;
    alert(themessage);                 

} 

问题是我想知道该产品是否已经存在于 Orders 表中,所以我可以更新它,而不是插入另一个具有相同代码的产品。

【问题讨论】:

  • 不确定为什么要引用单击和双击,如果您在移动设备上,您不会使用点击事件吗?我可以从您的代码中读取您正在执行一些您应该执行的事务回调函数中的警报是异步的,不会遵循其余函数逻辑的执行顺序。请更新您的答案,更好地描述您正在做什么以及您期望做什么。

标签: jquery html jquery-mobile


【解决方案1】:

不是真正的答案,但我重构了您的代码并添加了一些 cmets,您还应该查看我对您问题的原始评论,

function ListDBValues() {
    if (!window.openDatabase) { 
        alert('Databases are not supported in this browser.'); 
        return; 
    }
    $('#lbUsers').html(''); 
    db.transaction(function(transaction) { 
        transaction.executeSql('SELECT * FROM productos order by titulo;', [], 
        function(transaction, result) { 
            if (result != null && result.rows != null) { 
                for (var i = 0; i < result.rows.length; i++) { 
                    var row = result.rows.item(i);
                    // changed the append logic
                    // created new dom object w/jQuery
                    // bind tap event
                    // execute function on tap
                    // append to dom
                    $('<div id="producto"><img id="imgprod" width="100" src="images/' + row.foto +'">' + row.recid + '.' + row.titulo + '<br>$' + row.precio + ' MXP<br><input class="cuadro" type="button" id="cb.row" name="item"  value="ORDENAR"></div>').data( 'recid', row.recid ).bind('tap', function(){
                        AddValueToOrders( $(this).data('recid') );
                    }).appendTo('#lbUsers');
                } 
            } 
        }, errorHandler); 
    }, errorHandler, nullHandler);
    return; 
}
function AddValueToOrders(item) {  
    if (!window.openDatabase) { 
        alert('Databases are not supported in this browser.'); 
        return; 
    } 
    msga = "El item es: "+ item ;
    alert(msga);
    db.transaction(function(tx) { 
        /// veo si ya existe /////
        tx.executeSql('SELECT count(*) AS c FROM orders where prodid = '+ item +'  ', [], 
        function (tx, result) { 
            totprod = result.rows.item(0).c;
            //totprod = results.rows.item(0)['c'];
            alert("Total producto: " + totprod);
            // you can do your insert here depending on 
            // the value you got back from this checkpoint
        });
    });
}

如果效果更好,请告诉我。

更新: 此外,您在 for 循环中创建的元素上使用了 ID,您应该知道这会产生冲突,因为 ID 是唯一的,您可能不想将它们更改为类

【讨论】:

  • 嗨,非常感谢,它的工作方式与我预期的一样。你说得对,也许我不像移动应用那样思考,我像网络应用那样思考,所以我需要学习更多......,再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多