2

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

2 回答 2

2

您真的要在每个键入的键上或在用户完成键入时发布吗?大多数人可以在处理一个字母之前输入一个完整的单词。你需要一个计数。像这样的东西:

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);
    }
}
于 2011-08-23T21:28:26.997 回答
0

根据您的评论,听起来您想消除事件的抖动keyup。我推荐 Ben Alman 的jQuery throttle / debounce 插件

var itemNum = $('#item_num_id').val();
$('#textarea_id').keyup($.debounce(250, editItemInCart(this.value,'comments', itemNum)));

上面的代码消除了内联事件处理程序,为您提供了标记和代码之间的良好分离。

于 2011-08-23T21:33:37.467 回答