weixin_33713350 2011-08-23 21:08 采纳率: 0%
浏览 67

onkeyup方式太慢

http://jsbin.com/ezecun/edit#javascript,html

I have to write it this way because it is dynamicly made, actual code listed below. I simplified it a bit in the jsbin. Basically it takes so long to update the array with the value of the box it is unusable.

Thanks for taking a look.

CODE: 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);
}
  • 写回答

2条回答 默认 最新

  • 妄徒之命 2011-08-23 21:28
    关注

    Do you really want to post on each typed key or when the user finishes typing? Most people can type a whole word before one letter is processed. You need a count. Something like this:

    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);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!