2012-05-29 3 views
1

저는 HTML5/JqueryMobile에서 새로 생겼습니다. 나는이 기능을 가지고 있지만, 어떤 이상한 이유로, 그것은 내게 온 클릭()에 의해 호출 두 번째로 결과를 제공, 여기 내 코드입니다 :Jquery/Html5 함수 문제가 더블 클릭 문제입니다.

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 테이블에 이미 있는지 알고 싶기 때문에 업데이트하고 동일한 코드의 다른 제품을 삽입 할 수 없습니다.

+0

확실하지 않은 이유는 모바일에서 탭 이벤트를 사용하지 않는다면 클릭 및 더블 클릭을 참조 하시겠습니까? 콜백 내에서 알림을 수행해야하는 일부 거래를 수행하는 코드에서 읽을 수 있습니다. 함수이며 비동기 적이며 나머지 함수 논리에서 실행 순서를 따르지 않습니다. 귀하가하고있는 일과 기대하는 바를 잘 설명하여 답변을 업데이트하십시오. – rroche

답변

0

대답하지만 난 당신의 코드를 리팩토링 몇 가지 의견을 추가하지 정말, 당신은 또한

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 
     }); 
    }); 
} 

이 더 잘 작동하는지 알려 주시기 바랍니다, 귀하의 질문에 내 원래의 코멘트를 확인해야합니다.

UPDATE : 또한 당신이 for 루프를 작성하는 요소에 ID 년대를 사용하고, 당신은 ID 년대는 고유하기위한 것입니다, 당신은 클래스로 변경 wan't 질 수있는 충돌을 만듭니다 알아야한다

+0

안녕하세요, 고맙습니다. 일하기를 기대했던 것처럼 작동합니다. 그리고 당신 말이 맞아요, 아마도 모바일 앱처럼 생각하지 않아요, 웹 앱처럼 생각하고 있습니다. 그래서 더 공부해야합니다 ..., 다시 한번 감사드립니다. – CaribeSoft

+0

다행스럽게도 행운을 빈다. – rroche