I want to attach an event in the onChange and onEnter event of an input in a form. When user changes focus onChange event is called. But when user presses enter, the onEnter event is called, but the onChange event is also called, so I get my function executed twice. Any help would be appreciated. Thank you!!
...
with(form)
{
attachEvent("onChange", function (id, value){
buscaTarjeta();
});
attachEvent("onEnter", function(){
buscaTarjeta();
});
}
...
function buscaTarjeta()
{
with(frmTarjeta)
{
var tarj = getItemValue("codigo");
if(tarj)
{
load("data/tarjetas.php?op=get&codigo=" + tarj, function(){
if(!getItemValue("id"))
error("No se encontró ningún registro");
});
}
else
alertar("Ingrese un código de tarjeta para buscar");
}
}
This a complete demo where you can see my issue. If you type in a code and then click outside the input you get to see the message once. But if you try changing that code and then pressing Enter then the message will be shown twice.
I think a solution might be not calling the function onEnter, but changing the focus to another input. What would be the API call to do this? Or is there a way to cancel onChange event and executing only OnEnter event?
Thank you very much.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="cb/dhtmlx.js"></script>
<script type="text/javascript" src="cb/message.js"></script>
<link rel="stylesheet" type="text/css" href="cb/dhtmlx.css" />
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
overflow: hidden;
}
</style>
<script>
var cmbCobradores, gdTarjetas, frmTarjeta;
function setCobraDiario()
{
inlyt = new dhtmlXLayoutObject(document.body,"1C");
with(inlyt)
{
frmTarjeta = setFormCobraDiario(inlyt.cells("a"));
}
}
function buscaTarjeta()
{
with(frmTarjeta)
{
var tarj = getItemValue("codigo");
dhtmlx.alert("Buscando Tarjeta:" + tarj);
}
}
function setFormCobraDiario(cell)
{
var struc = [
{type: "settings", labelWidth:100, inputWidth:100, offsetLeft:10},
{type:"input", name:"codigo", label:"Código:", required:true}
];
var form = cell.attachForm(struc);
with(form)
{
attachEvent("onChange", function (id, value){
buscaTarjeta();
});
attachEvent("onEnter", function(){
buscaTarjeta();
});
}
return form;
}
</script>
</head>
<body onload="setCobraDiario();">
</body>
</html>