javascript保持循环 [英] javascript keeps looping

查看:253
本文介绍了javascript保持循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续这里,因为问题已明显(在我看来)



这里是我的代码

  (var i = 0; i (function(){
var queryid = results.rows.item(i).id; // sql primary key
var divid =#+ queryid; // assign id to divs
var pressTimer;
$(divid).mouseup(function(){// func处理tap + hold
clearTimeout (pressTimer)
//清除超时
return false;
})mousedown(function(){
//设置超时
pressTimer = window.setTimeout (){
alert(divid);
$(。somediv)。show();
$(#anotherdiv)hide();
$ #button)。on(click,function(){
var db = window.openDatabase(mydb,1.0,mydb,200000);
db.transaction(editrow);
function editrow(tx){
var value1 = $(#inputbox1)。val();
var value2 = $(#inputbox2)。val();
tx.executeSql(UPDATE mydb SET column1 =+ value1 +,column2 =+ value2 +WHERE id =+ queryid);
alert(divid); ********
successCB();} //查询函数
})
},1000)
return false;
});
})();
}

如果我点击div并且我编辑的值, ...
然而,如果之后我选择另一个div来更新它的字段,那么新的值被更新为选择的div,以及上一个选择的div ..



例如我选择div1并更新一切正常



如果之后我选择div2,div1和div2的值都更新div2的最近值

解决方案

您不止一次绑定事件。在div的每个mousedown中,您将一个click事件绑定到按钮。在jquery中调用on或click时,您不会设置事件处理程序,添加一个处理程序。



我不明白你是否需要调用setTimeout。



尝试这个aproach insted:

  var lastClickedId = null; 
function editrow(tx){
var value1 = $(#inputbox1)。val();
var value2 = $(#inputbox2)。val();
var queryid = lastClickedId;
tx.executeSql(UPDATE mydb SET column1 =+ value1 +,column2 =+ value2 +WHERE id =+ queryid);
alert(queryid);
lastClickedId = null;

successCB();
} //查询函数

$(#button)。on(click,function(){

if(lastClickedId!
{
var db = window.openDatabase(mydb,1.0,mydb,200000);
db.transaction(editrow);
}
});

for(var i = 0; i
(function(){
var queryid = results.rows.item ).id; // sql primary key
var divid =#+ queryid; //为divs分配id

$(divid).click(function(){
lastClickedId = queryid;
});
})();
}

UPDATE: b

使用 bind(func)在(func) ,等等,你注册的函数 func 在事件发生时被调用。您可以注册任意数量的函数。例如,如果你这样做:

  $(#div1)。on {alert(hello);}); 
$(#div1)。on(click,function(){alert(world);});

点击时会显示两个警报。这是因为jquery管理事件的方式。



要分离在 上连接到该元素(或元素)的所有点击事件,只要执行:

  $(#div1)。 

在purejavascript中,您可以这样做:

  var div1 = document.getElementById(div1); 

div1.onclick = function(){alert(hello); };
div1.onclick = function(){alert(world); };

在第二个示例中,只调用一个警报,因为您直接将值函数onclick。
并删除事件:

  div1.onclick = null; 


continuing from here ... since the problem has signifally altered (in my opinion)

here is my code

        for (var i = 0; i < len; i++) {
            (function () {
                var queryid = results.rows.item(i).id; //sql primary key
                var divid = "#" + queryid; //assigned id to divs
                var pressTimer;
                $(divid).mouseup(function(){ //func to handle tap + hold
                clearTimeout(pressTimer)
                // Clear timeout
                return false;
                }).mousedown(function(){
                // Set timeout
                pressTimer = window.setTimeout(function() {
                alert(divid);
                $(".somediv").show();
                $("#anotherdiv").hide();
                $("#button").on("click", function(){
                    var db = window.openDatabase("mydb", "1.0", "mydb", 200000);
                    db.transaction(editrow);    
                    function editrow(tx){
                        var value1 = $("#inputbox1").val();
                        var value2 = $("#inputbox2").val();
                        tx.executeSql("UPDATE mydb SET column1 = " + value1 + ", column2 = " + value2 + " WHERE id = " + queryid);
                        alert(divid); ********
                        successCB();} //query function
                })
                },1000)
                return false; 
                });
                })();
            }   

if i click on the div and i edit the values, they are submitted successfully... however, if after that i select another div to update its fields, then the new value is updated both to selected div, as well as to the previous selected div..

for instance i select div1 and update the values everything is ok

if after that i select div2 then both div1 and div2 values are update with more recent values for div2

解决方案

You are binding the events more than once. In every mousedown on the div, you are binding a click event to the button. You are not asigning the event handler when you call "on" or "click" in jquery, you are adding a handler.

I didn't understand if you need the call to setTimeout.

Try this aproach insted:

var lastClickedId = null;
function editrow(tx) {
    var value1 = $("#inputbox1").val();
    var value2 = $("#inputbox2").val();
    var queryid = lastClickedId;
    tx.executeSql("UPDATE mydb SET column1 = " + value1 + ", column2 = " + value2 + " WHERE id = " + queryid);
    alert(queryid);
    lastClickedId = null;

    successCB();
} //query function

$("#button").on("click", function(){

    if(lastClickedId != null)
    {
        var db = window.openDatabase("mydb", "1.0", "mydb", 200000);
        db.transaction(editrow);
    }
});

for (var i = 0; i < len; i++) {

    (function () {
        var queryid = results.rows.item(i).id; //sql primary key
        var divid = "#" + queryid; //assigned id to divs

        $(divid).click(function(){
            lastClickedId = queryid;
        });
    })();
}

UPDATE:

When you bind elements in jquery using bind(func), on(func), click(func), etc, you are registering the function func to be called whenever the event ocurrs. You can register any number of functions. So, for example, if you do:

$("#div1").on("click", function() { alert("hello"); });
$("#div1").on("click", function() { alert("world"); });

It will display two alerts when you click. This is because of the way jquery manages events. It calls every function you passed in the event.

To detach all the clicks events you atached to that element (or elements) with on, just do:

$("#div1").off("click");

In "pure" javascript you can do this:

var div1 = document.getElementById("div1");

div1.onclick = function() { alert("hello"); };
div1.onclick = function() { alert("world"); };

And in this second example, only one alert will be called, because you are directly asigning a value to the function onclick. And to remove the event:

div1.onclick = null;

这篇关于javascript保持循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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