http://jsbin.com/ezecun/edit#javascript,html
我必须这样写,因为它是动态制作的,下面列出了实际的代码。我在jsbin中稍微简化了一下。基本上,用框的值更新数组需要很长时间,它是不可用的。
谢谢你看。
代码:php
echo "<label style='float:left'>Comments: </label> <textarea onKeyUp='"editItemInCart(this.value,'comments',".$itemNum.")'" onChange='"editItemInCart(this.value,'comments',".$itemNum.")'" >".$cart['comments']."</textarea><br />";
javascript
function editItemInCart(newValue,fieldName,itemNum) {
jQuery.ajax({
type:"POST",
url: "editItem.html",
data: "newvalue=" + newValue + "&fieldname=" + fieldName + "&itemNum=" + itemNum,
})
//alert(newValue + fieldName + itemNum);
}
您真的想在每个键入的键上发布,还是在用户完成键入时发布?大多数人可以在处理一个字母之前键入一个完整的单词。你需要数数。类似这样的东西:
var count = 0;
function doEditItemInCart(newValue,fieldName,itemNum)
{
count++;
setTimeout("editItemInCart('"+newValue+"','"+fieldName+"',"+itemNum+","+count+")",200);
}
function editItemInCart(newValue,fieldName,itemNum,cnt) {
if (count == cnt) {
count = 0;
jQuery.ajax({
type:"POST",
url: "editItem.html",
data: "newvalue=" + newValue + "&fieldname=" + fieldName + "&itemNum=" + itemNum,
})
//alert(newValue + fieldName + itemNum);
}
}
根据您的评论,听起来您想取消对keyup
事件的通知。我推荐本·阿尔曼的jQuery节流器/debounce插件。
var itemNum = $('#item_num_id').val();
$('#textarea_id').keyup($.debounce(250, editItemInCart(this.value,'comments', itemNum)));
上面的代码消除了内联事件处理程序,使您可以很好地分离标记和代码。