Input onchange problem: 'refiring' a function
I'm adding products with its units to shopping cart via AJAX XmlHttp object. It does everything just fine but i have a problem when performing first succesful XmlHttp request: if i re-change units value on the input it re-fires the function, stucking on readyState 1.
It's like it had onchange but i am not explicitly calling the function with onchange event and i have checked that no script is referencing input id. Any thoughts about where to start looking for problem?
I have a form:
<form id="add-basket" name="add-basket">
<input id="udsn" name="udsn" class="add-units" type="text" value="1" />
<input type="button" id="prod-add" alt="Comprar" onclick="addProduct('<?=$product_id?>',1)"/>
开发者_StackOverflow中文版
And a function:
function addProduct(product,bsopt){
if (xmlHttp.readyState==4 || xmlHttp.readyState==0){
var uds = document.getElementById("udsn").value;
xmlHttp.open("GET", "addCart_process.php?prod=" + product + "&bso=" + bsopt + "&ud=" + uds, true);
xmlHttp.onreadystatechange = handleProductAdd;
xmlHttp.send(null);
}
}
Many thanks in advance.
If you have problems with such events then I would strongly suggest using a good AJAX framework like jQuery, MooTools, Prototype, YUI, Dojo, etc. There are lots of good libraries that would abstract the XMLHttpRequest API for you and also take care about browser inconsistencies. There's no need to reinvent the wheel if you don't feel comfortable doing it.
精彩评论